1

I wrote lots of code following XHTML rules, so I have things like this:

<input type="text" name="Field1" disabled="disabled">
<input type="text" name="Field2" readonly="readonly">

Thanks God, HTML5 went back to the old days where we could just do this:

<input type="text" name="Field1" disabled>
<input type="text" name="Field2" readonly>

However, how can I use jQuery to set or unset these boolean attributes? I always used .attr("readonly", "readonly"), but it generates the older XHTML syntax. I want my code to be the HTML5 way, with just "readonly", or just "disabled".

By the way, using .prop() won't work for me, I need to really change the attributes on my markup, because I have CSS rules depending on these attributes. Any ideas?

Thanks in advance.

1 Answer 1

1
    $('#inputId').attr('readonly', true);

I think you can also do...

$("#inputId").prop("readonly",true);
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your reply, but even setting it to true, the generated code is readonly = "readonly". I'm testing on Chrome. And using prop() doesn't change the markup on the document, so my CSS won't "see" it. Besides, when using prop, you have to use readOnly (capital O).
It's just for code uniformity. I'm using jQuery in so many places in my code, that I thought it would be weird to mix different approaches. Just that, there's not any stronger reason.
I guess what I mean, is that why don't you just use php to set that data?
Because I use ajax to change the input value, and while it's gathering the data, I use javascript / jQuery to keep the input in readonly state. And my CSS turns the field gray when it's in this state.
Ah I see.....well honestly, and this is just me, but I don't think I would even care if its not perfect HTML5 or whatever....as long as it works, then who cares....you're likely the only one to see or care about the code anyways. ;)
|

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.