I want to add an attribute to a HTML element without a value using JQuery
<input id="r1" type="radio name="r1" value="1">
Add required
<input id="r1" type="radio name="r1" value="1" required>
How can this be done without the =""
after the attribute?
I want to add an attribute to a HTML element without a value using JQuery
<input id="r1" type="radio name="r1" value="1">
Add required
<input id="r1" type="radio name="r1" value="1" required>
How can this be done without the =""
after the attribute?
-
1
don't worry about the
=""
, it's irrelevant just like the missing/
– Kevin B Commented Jun 19, 2013 at 21:47 -
$('input').attr('required','');
– Manish Mishra Commented Jun 19, 2013 at 21:48 -
@KevinB I believe
<DOCTYPE !html>
would be required, tho. – Ohgodwhy Commented Jun 19, 2013 at 21:48 -
why are there two
id
attributes – pid Commented Jun 19, 2013 at 21:51 - it's actually a boolean property and as such not required to have any value. You could add required='fred' and it would work the same. See :html5doctor./html5-forms-introduction-and-new-attributes – scrappedcola Commented Jun 19, 2013 at 21:56
1 Answer
Reset to default 8The standard practice would be to set the property to true
$("#r1").prop("required", true);
This actually results in markup that exactly reads
<input id="r1" required>
JSFiddle demo: http://jsfiddle/8SrED/