0
<html>
<body>

<select onfocus='blur(this);'>
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>

</body>
</html>

In firefox this works fine. In IE8, by clicking repeatedly you can get the options to appear. What gives? Anything else I can do to get the select to be readonly without showing it as disabled?

Anders
  • 8,307
  • 9
  • 56
  • 88
tim
  • 1,371
  • 3
  • 19
  • 30

2 Answers2

2

it would be onfocus="this.blur()"

but why would you do this? Just disable it. If you need the value, copy it to a hidden field

UPDATE:

<script>
function changeSel(idx) {
  var sel = document.forms[0].sel;
  sel.selectedIndex=idx;
  sel.form.selCopy.value=idx;
}
window.onload=function() {
  changeSel(document.forms[0].sel.selectedIndex);
}  
</script>
<form>
<input type="text" name="selCopy" value="not set" />
<select name="sel" size="1" disabled="disabled">
<option>Zero</option>
<option>One</option>
<option selected>Two</option>
<option>Three</option>
<option>Four</option>
</select>
<input type="button" onClick="changeSel(3)" value="Three" />
</form>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks. But same problem in IE8, you can still get the options to display. I am doing this because I am showing a popup when the select is clicked because I do not want the options list appearing below the select. I could not find a way to reposition it. – tim Apr 04 '11 at 12:25
  • @Tim so that means your question is wrong. Instead ask the question about what you want and not how to solve the problem you got when you decided on a specific workaround :) – mplungjan Apr 04 '11 at 12:30
  • @Tim basically it sounds like you do not want a select at all – mplungjan Apr 04 '11 at 12:40
  • What do you suggest I do? But still a legit ? on why this does not behave in IE8. – tim Apr 04 '11 at 12:42
  • I suggest you show us what you want to achieve and we suggest how to do it without trying to break something for the user. A simple text field should be fine. Why have a select if you do not want to show the selections? Anyway, what do you mean by "you can still get the options to display" if you have disabled="disabled" ??? – mplungjan Apr 04 '11 at 12:57
  • Why would you do this? Sometimes you need the readonly attribute versus disabled. In HTML5, you can't validate disabled fields, but you can validate readonly fields. – thecoolmacdude Oct 18 '17 at 19:16
  • When would you need to validate something the user cannot update? – mplungjan Oct 18 '17 at 19:19
1

You might want to go here: HTML form readonly SELECT tag/input

Community
  • 1
  • 1
korona
  • 2,308
  • 1
  • 22
  • 37