0

I am running into issues targeting a checkbox by it's id (serviceType-checkbox) and value (0) but am running into issues. Here is the code I have:

HTML:

<input type="checkbox" id="serviceType-checkbox" name="serviceType[]" value="0" />

jQuery:

target = jQuery("#serviceType-checkbox[value='0']");

Any help is much appreciated, thank you!

James P
  • 23
  • 4
  • 2
    each checkbox should have a different id and be put into an html
    . You want jQuery("#serviceType-checkbox");
    – clearshot66 May 15 '18 at 18:57
  • 2
    Why is the `value` portion necessary? Targeting it by ID should be all the qualification you need to isolate this single checkbox. – Tyler Roper May 15 '18 at 18:58
  • 3
    Why do you need to include the value here? You can just target by `id`. – David May 15 '18 at 18:59
  • 1
    To address the two comments above this one, perhaps the OP only wants to style that element if the value is 0. – j08691 May 15 '18 at 19:22
  • What kind of error do you get ? Because if I reproduce your code in jsFiddle like [here] https://jsfiddle.net/mbexf33a/ it works – Laura May 15 '18 at 19:43
  • Thanks @laura. I checked again and it is indeed working. Thank you! – James P May 15 '18 at 21:08

2 Answers2

0

JQuery return its own object, If you want to return the selected object then you have to select its first index like:

  target = jQuery("#serviceType-checkbox[value='0']")[0];

You can check it jsFiddle: https://jsfiddle.net/s30nvmpn/

0

Here you can try this:

var target = jQuery("#serviceType-checkbox[value='0']");
target.click(function(event) {
   alert(this.value);
});
Therichpost
  • 1,759
  • 2
  • 14
  • 19