1

Please inspect the following Console output, note that the 1st input has checked attribute and is selected:

enter image description here

You can see I have 2 radio inputs, 1 is checked, however neither selector nor prop nor is works for it. The code works correctly if I manually click on one of the radio. Here is my HTML:

<div class="col-sm-6">
    Type:
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-default active">
            <input type="radio" name="generator-type" id="opt-type-passphrase" value="0" autocomplete="off" checked>
            Passphrase
        </label>
        <label class="btn btn-default">
            <input type="radio" name="generator-type" id="opt-type-password" value="1" autocomplete="off">
            Password
        </label>
    </div>
</div>

Why is the code not working when the page loads when I haven't clicked any radio? Is there a way to fix it?

EDIT: Found the problem, I have this code in the page load:

        $("input[name='generator-type'][value=" + this.Options.Type + "]").prop("checked", "");

The correct solution is using:

.prop("checked", true)

But can anyone please explain how it work? Why is there the checked attribute, but prop function is still false?

2
  • @HectorBarbossa Thank you, I found the source of problem, but still not understand how prop and is work yet. Could you explain it? Commented Jun 29, 2016 at 21:42
  • 1
    Your question regarding how it works is covered in the documentation. One key aspect to grasp: The HTML attribute only sets the initial value of the property. If it is changed via code or user interaction, the HTML doesn't change, but the property does. Commented Jun 29, 2016 at 21:50

3 Answers 3

3

Try .is(':checked') instead of .is('checked'). Please check this Fiddle.

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

Comments

1

You need to check like this

$( ... ).is(':checked')

Why? Because the jquery is method accepts a css selector which it uses to filter the currently matched elements.

Thus temp.is('checked') will only be true if the temp elements have the checked tag. In your case they have the input tag so do not match.

Using :checked matches on the css pseudo selector for whether the current element is checked or not. This is what you want.

Knowing this, you could also use $( ... ).is('[checked]'), which will return true if the matched elements have the checked attribute. That's probably what you were trying to do originally. Though, the conventional way is to use :checked.

1 Comment

Good catch. Updated.
1

You're missing a colon in the is-checked code:

temp.is(":checked")

instead of

temp.is("checked")

1 Comment

How is this different from the existing answer?

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.