Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Setting attribute required for input tag

I was trying to set attribute to an input tag using jQuery. All other attributes for input tag like maxlength,name,class worked well. But i'm having trouble with required attribute.

I tried this way

 $("<label><strong>Class"+fare+"</strong></label><input type='text' value='' />")
.attr("class", "nameval") .attr("name", "class"+fare+"") .attr("required","required")

I also tried other ways refering to some forums

.attr("required","")
.attr("required",true)
.attr("required","true")
.prop("required",true)

Please let me know where i was wrong.

like image 707
Umesh Varma Avatar asked Jun 16 '26 22:06

Umesh Varma


1 Answers

You can try like this.

var element = $("<label><strong>Class"+fare+"</strong></label><input type='text' value='' />")
.attr("class", "nameval") .attr("name", "class"+fare+"") .attr("required","required");

$("#MyFormId").append(element);
like image 114
sudhAnsu63 Avatar answered Jun 19 '26 12:06

sudhAnsu63



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!