2

I am experiencing trouble as I want to make checkbox value checked when site loads and the visitor can uncheck it. id field is dynamic inside input so I need to check based on name and value I suppose.

I used jQuery to make it checked, but nothing happens

jQuery(document).ready(function($) { 
 $("input.myclass[name='test-case'][value='yes']").prop("checked", true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="test-case[]" id="cbopt-AAljenwo" value="yes">

Not sure if this is correct value of marking input as checked. User also can uncheck box if he doesn't like the option. Any help appreciated.

3limin4t0r
21.4k3 gold badges38 silver badges61 bronze badges
asked Apr 16, 2020 at 12:26
7
  • 1
    1) Look at the name in the HTML and the name you've set in the selector. 2) there's no class on that checkbox element 3) If you want this to work on load, just use checked="checked" in HTML Commented Apr 16, 2020 at 12:27
  • Use good old html and add checked attribute to the checkbox Commented Apr 16, 2020 at 12:27
  • 3
    you've got an id. why not use that? Commented Apr 16, 2020 at 12:28
  • @Yishmeray id field is dynamic inside input so I need to check based on name and value Commented Apr 16, 2020 at 12:29
  • @RoryMcCrossan not able to insert code in HTML as this is generated through builder. But can add jquery code which will mark it as checked. Not sure how to do it Commented Apr 16, 2020 at 12:36

3 Answers 3

3

The best way to achieve this would be to put a checked attribute in the HTML. However you state that the HTML is being generated for you, so you don't have that option.

The second best option would be to target the checkbox by its id, however that has the same issue for you as the id is dynamically generated at runtime.

Your last resort is to then select the element by its name and value, which is what your jQuery is trying to do, however there's a couple of issues. Firstly the checkbox you're targeting has no class and the name attribute has a [] suffix which you need to include. Try this:

jQuery($ => {
 $('input[name="test-case[]"][value="yes"]').prop('checked', true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="test-case[]" value="yes">

answered Apr 16, 2020 at 12:39
Sign up to request clarification or add additional context in comments.

Comments

3

You can simply add checked attribute to the input element to make it checked when the page is loaded:

<input type="checkbox" checked="checked"/>

Thanks

Rory McCrossan
338k41 gold badges321 silver badges353 bronze badges
answered Apr 16, 2020 at 12:31

1 Comment

<input type="checkbox" checked /> would be enough. There is no reason to assign a value to the attribute. "checked - Boolean; if present, the checkbox is toggled on by default" - MDN
0

simply that:

const theChexbox = document.querySelector("input[name='test-case[]']")
theChexbox.checked = true
<input type="checkbox" name="test-case[]" id="cbopt-AAljenwo" value="yes">

answered Apr 16, 2020 at 12:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.