1

I am looking to check a specific check box by calling its name. The name is c[featured_ad] and I will share what I've attempted below. Thanks for your help.

JavaScript:

<script type="text/javascript">
window.onload = function() {
$('input:checkbox[name=c[featured_ad]]').attr('checked',true);
}
</script>

HTML:

<div class="listing_extra_item">
<div class="listing_extra_cost price">$0.00 US</div>
<input name="c[featured_ad]" value="0" type="hidden">
<label>
<input name="c[featured_ad]" value="1" type="checkbox">
</label>
</div>
3
  • Possible duplicate of stackoverflow.com/questions/3462344/… Commented Dec 1, 2013 at 1:26
  • Anyone else have any ideas? So far, none have worked. Commented Dec 1, 2013 at 4:01
  • What are you getting in the console when you try to run the script? Any errors? Commented Dec 1, 2013 at 12:41

3 Answers 3

1

You can use:

$('input[type="checkbox"][name="c[featured_ad]"]:checked').length > 0;

to check if there is any checkbox checked.

If you need to check a checkbox on document load, just use:

$('input[type="checkbox"][name="c[featured_ad]"]').prop('checked', true);

And most important of all remember that single quotes should be used where you want double quotes to appear inside the string without having to escape them, or vice versa.

Check this fiddle: http://jsfiddle.net/65adr/51/

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

Comments

0

Here try this:

http://jsfiddle.net/r6WLE/1/

JS

$("input[type='checkbox'][name='c[featured_ad]']").attr('checked', 'checked');

Also, close all the HTML tags, it's just a better practice.

4 Comments

Thanks but not working. I am adding the JS immediately before the closing </head>. I tried a quick alert( "welcome" ); and that worked like a charm.
Did you use document ready function and place it inside it ? Works fine on fiddle
I saw that it worked fine in Fiddle which makes this more perplexing. Originally, I had it run on window onload, I changed to document ready. How does this look? <script type="text/javascript"> $(document).ready(function(){ $("input[type='checkbox'][name='c[featured_ad]']").attr('checked', 'checked'); }); </script>
Did you include the js library ? Do you see any error in consoles ?
0

Try putting the name in quotes:

$('input:checkbox[name="c[featured_ad]"]').attr('checked', 'checked');

If you are on jQuery 1.6+ you should use .prop():

$('input:checkbox[name="c[featured_ad]"]').prop('checked', true);

Edit:

Apparently you need to escape the brackets:

$('input:checkbox[name="c\[featured_ad\]"]').prop('checked', true);

5 Comments

Thanks for replying so quickly. Doesn't look like that worked.
Edited answer, try escaping [] with using double backslash.
Escaping didn't do it. I also tried it with both methods, .prop and .attr
Hmm, apparently stackoverflow escaped my escapes! so its \\ aka. backslashbackslash..
I attempted with double backslash and I am not successful. Check it out. $(document).ready(function(){ $('input:checkbox[name="c\\[featured_ad\\]"]').prop('checked', true); }); We've gotta be close. These suggestions make sense and they're checking out in Fiddle too.

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.