1

I have an input looking like this:

<input type="text" value="1.1" size="5" name="Topic" id="Topic">

What's the best way to change it so this input is disabled using jQuery?

Update

Sorry I forgot to ask but is there also a way that I can make it read only. The original answers are great but this is just one more thing I need to be able to do.

3 Answers 3

3

jQuery >= 1.6:

$("#Topic").prop("disabled", true);

jQuery < 1.6:

$("#Topic").attr("disabled", "disabled");
Sign up to request clarification or add additional context in comments.

2 Comments

Is setting prop a new way to set attributes. I didn't see this before. Also I'm sorry I forgot to ask. Is there also a way that I can make it read-only?
You can do the same thing, but with "readonly" instead of "disabled". Yes, .prop() sets dynamic properties, whereas .attr() sets attributes, which are read only once at page load time. See api.jquery.com/prop
1

Set the attribute disabled="disabled":

$("input#Topic").attr("disabled", "disabled");

Comments

1

Just have to set the disabled attribute:

$('#myinput').attr('disabled', 'disabled');

To re-enable it, you can:

$('#myinput').removeAttr('disabled');

1 Comment

Thanks very much. Is there also a way that I can make it read-only? I am not sure but I want to get my form values back. If it is disabled will the values be returned?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.