Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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

I think you can also do...

$("#inputId").prop("readonly",true);
share|improve this answer
    
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). –  Marcovecchio Jun 8 '13 at 2:23
    
Question....why are you using Jquery to set the properties? Just asking.. –  KyleK Jun 8 '13 at 2:24
    
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. –  Marcovecchio Jun 8 '13 at 2:29
    
I guess what I mean, is that why don't you just use php to set that data? –  KyleK Jun 8 '13 at 2:34
    
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. –  Marcovecchio Jun 8 '13 at 2:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.