Reputation: 20561
I want to retrieve the text of a button that is clicked. I've read up on other suggestions for similar questions, but cant seem to get it to work. Here is the fiddle: http://jsfiddle.net/z3PKm/63/ Which currently isn't working. What am I doing wrong, or what should I do which is right? Thanks! I've tried to copy the following solution
alert($(this).attr("value"));
But it still doesn't work for me.
Upvotes: 0
Views: 72
Reputation: 7666
You need to use alert($(name).text());
I have updated the jsfiddle with the same:
jsfiddle
Upvotes: 0
Reputation: 1598
This is how I would do it jQuery style.
This also show you how to use inputs and buttons and text()
or val()
as appropriate
$('.choose').on('click', function(){
var $this = $(this);
alert('text: '+$this.text()+'\nval: '+$this.val());
}); // end on click
Upvotes: 0
Reputation: 33870
The type of button that you are using are not using value
, they are text.
Write this:
$(name).text()
Upvotes: 3
Reputation: 123739
Use this:-
.text()
will give you the text of the button
as per your example. You are using button
and not input type="button"
If it was input type="button"
then .val()
would work.
function test(name) {
alert($(name).text());
}
Since you are using jquery you can use the click event
$('button').on('click',function(){
alert($(this).text());
});
Upvotes: 3