Solved

JQuey get element by name not value

Posted on 2014-03-20
7
372 Views
Last Modified: 2014-03-20
The following script does not work:  If I grab the element by its Value it works, but it does not work when I grab it by the name.  Any Help?

$(document).ready(function() {

	$('input[type="button"]').click(function() {
		if ($(this).name() == "custom_1_yes") {
            $(this).text("Finish Customizing");
			$("#custom-hidden").toggle('slow');
		}

		if ($(this).name() == "custom_2_yes") {
            $(this).text("Finish Customizing");
			$("#custom-hidden-2").toggle('slow');
		}
  });

<input type="button" name="custom_1_yes" value="CUSTOMIZE NOW" />

Open in new window

0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39943506
What about :
$(document).ready(function() {

	$(':button[name^="custom_"]').click(function() {
			$(this).text("Finish Customizing");
			var x = $(this).attr("name").replace(/\D/g,"");
			x = x?"-"+x:"";
			$("#custom-hidden"+x).toggle('slow');
	});

});

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39943516
Or :
$(document).ready(function() {

	$(':button[name^="custom_"]').click(function() {
			$(this).text("Finish Customizing");
			var toHide = "#" + $(this).data("hide");
			$(toHide).toggle('slow');
	});

});

<input type="button" name="custom_1_yes" value="CUSTOMIZE NOW" data-hide="custom-hidden" />
<input type="button" name="custom_2_yes" value="CUSTOMIZE NOW" data-hide="custom-hidden-2" />

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 39943578
@leakim971 I did the following and it works great.  However, when I click the button, the value on the button changes.  That is good.  However, when I click the button again, it does not go back to the original value.

$(':button[name^="custom_"]').click(function() {
			$(this).val("Finish Customizing");
			var x = $(this).attr("name").replace(/\D/g,"");
			x = x?"-"+x:"";
			$("#custom-hidden"+x).toggle('slow');
	});

Open in new window

0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 82

Expert Comment

by:leakim971
ID: 39943628
I did the following and it works great.
Bad choice :)

why should it get the original value if you overwrite it?

$(':button[name^="custom_"]').click(function() {

var strFC = "Finish Customizing";
var bDECISION = $(this).data("original") != strFC; // true or false
var text = bDECISION?strFC:$(this).data("original");
var sShow = bDECISION?"block":"none";

			var x = $(this).attr("name").replace(/\D/g,"");
			x = x?"-"+x:"";
			$("#custom-hidden"+x).css("display", sShow);

	}).each(function() {

$(this).data("original", $(this).val());

});

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 39943782
Because when the div closes, I would like to see the original value.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39943790
did you try the new code?
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39943800
Yes, but I think you were right in the first place.  That you !!!!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question