Solved

JQuey get element by name not value

Posted on 2014-03-20
7
355 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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
Because when the div closes, I would like to see the original value.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
did you try the new code?
0
 
LVL 7

Author Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now