3

Hi I am wondering why my checkbox won't check when I tell it to do so during a page load. Here's what I have:

if($('#contentPlaceholder_C003_ctl00_ctl00_C010_ctl00_ctl00_checkBoxes_0').is(":checked")==false){
   alert('works');
   $('input:checkbox[value="Sample 1"]').prop("checked", true);
}

It gets to the alert but after that it won't check the property.

EDIT Here's part of the HTML

<input id="contentPlaceholder_C003_ctl00_ctl00_C010_ctl00_ctl00_checkBoxes_0" type="checkbox" name="ctl00$contentPlaceholder$C003$ctl00$ctl00$C010$ctl00$ctl00$checkBoxes$0" value="Sample 1">

if I do like a click event using the same selector, it works.

EDIT2 It works in JSfiddle. I guess it has to do with Sitefinity. I'll have to take a look into it.

13
  • Why don't you just do <input type="checkbox" id="test" checked="checked"> that will check it initially...
    – brso05
    Commented Apr 6, 2015 at 13:21
  • try with document.ready Commented Apr 6, 2015 at 13:21
  • I'm getting undefined is not a function when the page loads, so I think your selector inside the if is wrong. checking now.
    – Zack
    Commented Apr 6, 2015 at 13:22
  • Your code seems to work. Do you have the value of the target input set to `"Sample 1"?
    – Teemu
    Commented Apr 6, 2015 at 13:23
  • 2
    Could you provide your html? Commented Apr 6, 2015 at 13:23

5 Answers 5

0

your code is working maybe you don't add jquery library..

i just put a test code to see it

<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<input type="checkbox" value="Sample 1" id="contentPlaceholder_C003_ctl00_ctl00_C010_ctl00_ctl00_checkBoxes_0" >test1</input>

<input type="checkbox" value="Sample 2" id="contentPlaceholder_C003_ctl00_ctl00_C010_ctl00_ctl00_checkBoxes_0" >test2</input>

<script> 



if($('#contentPlaceholder_C003_ctl00_ctl00_C010_ctl00_ctl00_checkBoxes_0').is(":checked")==false){
   alert('works');
   $('input:checkbox[value="Sample 1"]').prop("checked", true);


}

 </script>
</body>

</html>
1
  • OP says the alert is shown, hence the jQuery also has to be loaded already.
    – Teemu
    Commented Apr 6, 2015 at 13:42
0

Your code works fine, as you can see in the snippet below. Just make sure you are including the jQuery 1.6 library at a minimum, because that is when the prop() method was added.

https://api.jquery.com/prop/

if ($('#contentPlaceholder_C003_ctl00_ctl00_C010_ctl00_ctl00_checkBoxes_0').is(":checked") == false) {
  alert('works');
  $('input:checkbox[value="Sample 1"]').prop("checked", true);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="contentPlaceholder_C003_ctl00_ctl00_C010_ctl00_ctl00_checkBoxes_0" type="checkbox" name="ctl00$contentPlaceholder$C003$ctl00$ctl00$C010$ctl00$ctl00$checkBoxes$0" value="Sample 1">

0

use .attr()

$('input:checkbox[value="Sample 1"]').attr("checked", true);
1
  • This does not work. Using prop instead of attr works though.
    – Zack
    Commented Apr 6, 2015 at 13:44
0

Try this:

$('input:checkbox[value="Sample 1"]').prop("checked", "checked");
0

Probably a Sitefinity code is running after your code and overrides your value.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.