Skip to content Skip to sidebar Skip to footer

Append Attributes To Input Element Using Jquery

If I have input element rendered like this

Solution 1:

If you want to add an attribute to an element try.

$('#Requester').attr("data-val-required","The Requested value field is required.");

Solution 2:

Try this:

$("#Requester_Value").attr("data-val-required","The Requested value field is required.");
$("#Requester_Value").attr("data-val-number","The field Requester must be a string.")

Solution 3:

You can use .attr() to get set attribute value:

$('#Requester').attr({'data-val-required':'The Requested value field is required.' ,'data-val-number':'The field Requester must be a string.'});

.attr() with data-* will set the data property as well as attribute value for that element. whereas .data() will only set data property internally and values wont be reflected in attributes.

Solution 4:

You can use .attr() to set an attribute of an element, see the jQuery documentation here

    $('#Requester_Value').attr("data-val-required","The Requested value field is required.");

Post a Comment for "Append Attributes To Input Element Using Jquery"