javascript - Add HTML Attribute without a value using Jquery - Stack Overflow

I want to add an attribute to a HTML element without a value using JQuery<input id="r1" ty

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?

Share Improve this question edited Jun 19, 2013 at 21:53 ojhawkins asked Jun 19, 2013 at 21:43 ojhawkinsojhawkins 3,29816 gold badges53 silver badges69 bronze badges 6
  • 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
 |  Show 1 more ment

1 Answer 1

Reset to default 8

The 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/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742389773a4434778.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信