Search code examples
jqueryhtmltags

Adding attribute in jQuery


How can I add an attribute into specific HTML tags in jQuery?

For example, like this simple HTML:

<input id="someid" />

Then adding an attribute disabled="true" like this:

<input id="someid" disabled="true" />

Solution

  • You can add attributes using attr like so:

    $('#someid').attr('name', 'value');
    

    However, for DOM properties like checked, disabled and readonly, the proper way to do this (as of JQuery 1.6) is to use prop.

    $('#someid').prop('disabled', true);