samuraiseoul
samuraiseoul

Reputation: 2967

Select the value tag of the element selected by class

I have a page that has a whole bunch of buttons

<button value=<?php val?> class='hot' >Hot</button>

and Im selecting them by class because the values will all be different. Im trying to have them, on click, alert the value, but I can't figure out how to access the value field.

My function is

$(".hot").click(function() {
});

and I tried to access it(after much time spent googling) using:

$(this).find('option:selected').attr('value');

resulting in

$(".hot").click(function() {
     $(this).find('option:selected').attr('value');
});

however, when I alert that, it just says undefined.

And ideas would be awesome! Im pretty familiar with php, but javascript and JQuery are pretty new to me, so links and good explanations are really appreciated, not just an answer. Thanks!

Upvotes: 0

Views: 87

Answers (3)

Ankush Jain
Ankush Jain

Reputation: 7069

Try This one...It will alert the value of button which was clicked...

$(".hot").click(function() {
alert($(this).val());
});

Upvotes: 1

moonwave99
moonwave99

Reputation: 22810

You just need:

$(".hot").click(function(e) {
   alert( $(this).attr('value') );
});

Upvotes: 1

Laurence
Laurence

Reputation: 1713

Use .val():

$(".hot").click(function() {
   var valueOfButton = $(this).val();
});

Please also see this other SO question for the difference between .val() and .attr('value'): What's the difference between jQuery .val() and .attr('value')?

Upvotes: 5

Related Questions