8

I have got this checkbox which has value 1.

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

Also I use this method to make checked/unchecked it.

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

    // Get 'VALUE' of the checkbox here   

});

What I need is somehow get 'VALUE' of the clicked checkbox. So In that case it should be 1.

Any clue how do it could be done?

Thank you!

1

4 Answers 4

19

In your click method use this to get the value

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

$(this) is referencing to the object that has been clicked.

EDIT: you could also use $(this).val(); but sometimes I had problems with elder versions of IE so I did recommend $(this).attr("value") in the first place.

Sign up to request clarification or add additional context in comments.

Comments

1
​<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/

Working :)

Comments

1

I think you just need $(this).val(); in your click event.

Also, if you need to work with that value as an int later, use

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

Comments

0

The val jQuery method should see you right.

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.