?
Solved

JQuey get element by name not value

Posted on 2014-03-20
7
Medium Priority
?
383 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 2000 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
Suggested Courses

762 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