Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add HTML Attribute without a value using Jquery

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?

like image 736
ojhawkins Avatar asked Mar 25 '26 10:03

ojhawkins


1 Answers

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.net/8SrED/

like image 144
compid Avatar answered Mar 28 '26 01:03

compid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!