Solved

JQuey get element by name not value

Posted on 2014-03-20
7
369 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node.js 11 69
Jquery Ajax - on change event not being picked up 9 55
JSON help 8 40
Error just appeared 3 18
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…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
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…

740 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