tags:

views:

35

answers:

2

now , this my code

<button class="delclass id="b1"
name="b1" value="b11">del</button>

 <script type="text/javascript">
         $(document).ready(function () {
             $('.delclass').click(
                 function () {
                     alert('1111');
                     alert($(this).attr('value'));
                     return false;
                 });
         });
         </script>

<button class="delclass id="b1"
name="b1" value="b11">del</button>

I only want to get button's value (e.g. b11), I don't want to write:

 $('.delclass').click 

I only want to write:

$(this).attr('value')

but it doesn't work (displays 'undefined').

+2  A: 

In jQuery it's:

$(this).val(); //to fetch value
$(this).val('newValue'); //to set a new value

To get the value of the button without using .click, do:

$("button[name='b1']").val();  //by name
$('#b1').val(); //by id

Check out the docs at: http://api.jquery.com/val/

Jon Weers
A: 

If you do not wrap it inside a function that is bound on the element, the this will refer to the window object and not your element..

use alert( $(#'b1').val() ); instead

Gaby