Adding attribute in jQuery

Yuda Prawira picture Yuda Prawira · May 13, 2011 · Viewed 578.2k times · Source

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

Answer

Paul Rosania picture Paul Rosania · May 13, 2011

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);