jQuery: Best way to disabled input

Which is best?
$('#xyz').attr("disabled", true); 
$('#xyz').attr("disabled", "true"); 
$('#xyz').attr("disabled", "disabled"); 

Open in new window

LVL 16
Who is Participating?
ProculopsisConnect With a Mentor Commented:
I prefer:

$('#xyz').attr( { disabled: true } );

because you can add more stuff:

$('#xyz').attr( { disabled: true, value: "Locked" } );

works with prop too:

$('#xyz').prop( { disabled: true } );
$('#xyz').prop( { disabled: true, value: "Locked" } );
leakim971Connect With a Mentor PluritechnicianCommented:
$('#xyz').attr("disabled", "disabled"); ¿
or :
$('#xyz').attr("disabled", ""); ¿


in the jQuery plugin you can find this line :
elem.setAttribute( name, "" + value );

Open in new window

where elem is your input
name is the disabled attribute
the second argument is a string
robertarkConnect With a Mentor Commented:
Use .prop() - http://api.jquery.com/prop/

$('#elem').prop('disabled', true)
$('#elem').prop('disabled', false)

Open in new window

Kiran SonawaneConnect With a Mentor Project LeadCommented:
If you are using latest version of jquery then use prop otherwise $('#xyz').attr("disabled", "disabled");
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.