Get value attribute of clicked checkbox via jQuery

I have this one checkboxthat has a value of 1 .

<input type="checkbox" name="option_1" id="checkbox_1" value="1">

I also use this method to do check / unchecked.

$('input[id^="checkbox_"]').not('#checkbox_all').click(function () {
            $('#checkbox_all').prop('checked', false);

    // Get 'VALUE' of the checkbox here   

});

I need to somehow get the "VALUE" clicked checkbox. Therefore, in this case it should be 1 .

Any clues how to do this?

Thank!

+5
source share
4 answers

In the click method use this to get the value

$(this).attr("value");

$(this) refers to the object that was clicked.

EDIT: you can also use $(this).val();, but sometimes I had problems with older versions of IE, so I recommended it $(this).attr("value")first.

+14
source
​<html>

​<head>

​</head>

<body>
    <input type="checkbox" name="option_1" id="checkbox_1" value="1">
</body>

​</html>​​​​​​​

$('input[id^="checkbox_"]').not('#checkbox_all').click(function () {
            $('#checkbox_all').prop('checked', false);

   alert($(this).val());  

});​

http://jsfiddle.net/heten/

:)

+1

, $(this).val(); click.

, int ,

var x = parseInt($(this).val(),10);

+1

The valjQuery method should see you correctly.

$('#checkbox_all').val();

http://api.jquery.com/val/

0
source

All Articles