0

I've got a problem, i want to add "checked" when i click on the input but I don't know how to do, here is my jQuery code :

jQuery('#myId div input').click(function(){
                                jQuery(this).attr('checked', 'checked');
                            });

And this is my HTML code :

<form id="myId">
<div class="grid12-12">
<input type="radio" value="292" id="292" class="required-entry">
<label for="292">Ordonnance.jpg</label>
</div>
</form>

but the thing is that if I do that, this is what happens, I have this in my tag :

checked = "checked"

And I want only "checked" in my tag like this

<input type="radio" id="292" value="292" checked>

if I remove the checked in the jQuery like this :

jQuery('#myId div input').click(function(){
                                jQuery(this).attr('', 'checked');
                            });

I have an error.

So how can I do that please ? Thanks in advance :)

3 Answers 3

1

Use prop instead of attr, because this is a property, not an attribue.

$('#myId div input').click(function(){
    $(this).prop('checked', true);
});

But it is correct that you see checked="checked". This is the same with using disabled, it will become disabled="disabled". No problem at all. There are even HTML versions where this type of attribute minimization is forbidden, like XHTML. Therefore it is the savest way to use checked="checked".

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

Comments

0

This is how you solve it.

$( "#myId div input" ).prop( "checked", true ); $( "#myId div input" ).prop( "checked", false );

Comments

0

You are passing an empty attribute to the element its just like saying

<input type="radio" id="292" value="292" ="checked " />

So you do it like this

$(this).attr("checked", true);

Comments

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.