Readonly does not work

Asked

Viewed 443 times

-1

By dynamically including an item in table, I want to leave the field cbempresa with the readonly=true and when there are no items in the table he needs to be readonly=false , logic works when I use disabled, but readonly nay. Go on like I’m doing:

$("#cbempresa").prop("readonly", true);

And here is to remove the readonly:

  $("#cbempresa").prop("readonly", false)

I thought it was something I was doing wrong, but by putting disabled works.

  • It should work. Maybe the context is preventing something.

  • 3

    Mariana, could you inform in your question how is the inclusion of a new item (Is the inclusion of a new row in the table?, how it is made?) and if possible also tell us what element of html is "cbempresa" because there are elements that are not compatible with the readonly such as the <select>

  • @Sam this happens, when I’m adding data dynamically into table, thought it might be something that was preventing, but because it would prevent the readonly and would not prevent the disabled?

  • @Caiqueromero the input is select so it doesn’t work readonly ?

  • 2

    The element <select> does not have the attribute readonly, see here. And the attribute readonly does not receive a value true or false, if it is present, the element is read only, if it is not present it is not, see here.

  • I think there is a question with this scenario. I’m trying to find here.

  • Take a look here: https://answall.com/a/273982/8063

  • @Sam but in this case may confuse the user, because the value will be of another that he selected, however it can change, it is not that, or got it wrong ?

  • Mariana, in the example I showed, the user can open select but cannot change the selected option. It would be an emulation of readonly, since in the disabled the user can’t even open select.

  • 2

    Sorry for the delay, just for clarification, select and input are different elements. Peter has already answered your question and left excellent references for a better understanding. Now on how to deal with this situation, if you really need to use one select you can simulate a readonly when removing possible user interactions with the field, follow the link with a few suggestions: https://answall.com/questions/128188/como-aplica-readonly-em-um-select

Show 5 more comments

1 answer

5


The element <select> (as in the case of its #cbempresa) does not have the attribute readonly, see here:

<select> HTML Element - Attributes

And the attribute readonly does not receive a value true or false, if it is present, the element is read only, if it is not present it is not, see here:

What is the Difference between readonly="true" & readonly="readonly"? - Stack Overflow

See an example below, the field nascimento is not read only and the field idade is read-only:

<input type="text" id="nascimento" name="nascimento">
<input type="text" id="idade" name="idade" readonly>

EDITION

As mentioned in the comments (Sam and Caique Romero), there are some alternatives to simulate the behavior readonly in one element <select>, see here:

How to apply readonly in a select? - Stack Overflow

  • I understand, thank you very much for the explanation, but I chose to put as disabled and when submitting the form, I withdraw the disable to pick up the field. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.