1

So I'm trying to check a box by value that's equal to the variable but I can't get to work. Here's a fiddle: https://jsfiddle.net/OpuLance/n3L8a4zz/4/

It checks all the boxes if I set it to .val(checkboxValue).prop("checked", "checked");

I tried .val() == checkboxValue.prop("checked", "checked"); but that doesn't work

OpuLance
  • 461
  • 5
  • 16
  • You are looking for the attribute selector: https://api.jquery.com/attribute-equals-selector/. `.val` either sets the value or returns the value of the first element in the set. – Felix Kling Apr 08 '15 at 14:07
  • Related: http://stackoverflow.com/questions/4893436/jquery-selectors-with-variables – Felix Kling Apr 08 '15 at 14:09

2 Answers2

1

Use attribute selector in jQuery:

$('input[value="'+checkboxValue+'"]:checkbox').prop("checked", "checked");

fiddle

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Balachandran
  • 9,567
  • 1
  • 16
  • 26
0

you are checking all the check box by $('input:checkbox').val(checkboxValue).prop("checked", "checked");
use $('input[value="'+checkboxValue+'"]:checkbox').prop("checked", "checked"); demo

ozil
  • 6,930
  • 9
  • 33
  • 56