Solved

JQuey get element by name not value

Posted on 2014-03-20
7
367 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

830 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