Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop down selection is still selectable with "readonly" attribute

I want a read-only "select" element to be not selectable, the same behavior as the readonly input box.

In the code below, you cannot change the value for the input box with the value "abc". However, you can still change the selection in the drop. I can't use "disabled" attribute because I still need to send these values to the server.

<input type="text" readonly="readonly" value="abc">

</input>

<select readonly="readonly">
  <option>Item ABC</option>
  <option>Item XYZ</option>
</select>

https://jsfiddle.net/6Lu1jpLx/

like image 861
user1187968 Avatar asked Sep 15 '16 18:09

user1187968


2 Answers

Simplist way to resolve this would be to use the below line:

$("#MySelect").css("pointer-events","none");

However, the following worked for me where in my case I wanted my Select to still have the disabled cursor - setting 'pointer-events' to 'none' will no longer show cursor as 'not-allowed'.

JQUERY

var isReadOnly = $("#MyCheckbox").prop("checked"); 
var domElements = $("#MyInput, #MySelect");

$(domElements).prop("readonly", isReadOnly);
$(domElements).toggleClass("my-read-only-class", isReadOnly);
$(domElements).find("option").prop("hidden", isReadOnly);

CSS

.my-read-only-class 
{   
   cursor: not-allowed;
}

JSFiddle https://jsfiddle.net/xdddzupm/

like image 167
Claire Avatar answered Oct 20 '22 01:10

Claire


style="pointer-events: none;"

Is worked for me

<select style="pointer-events: none;">
  <option>Item ABC</option>
  <option>Item XYZ</option>
</select>
like image 28
HSP Avatar answered Oct 20 '22 01:10

HSP