2

Please refer this link

Demo

Please help me to make this listbox readonly. Please do not use disable attribute as I need to post it later.

 <select size=4 multiple="multiple">
    <option selected=selected>Volvo</option>
    <option selected=selected>Saab</option>
    <option selected=selected>Mercedes</option>
    <option selected=selected>Audi</option>
  </select>
4
  • What do you mean by read only? Do you mean it should behave as though disabled, without using the disabled attribute? Commented May 15, 2014 at 12:30
  • 1
    stackoverflow.com/questions/368813/… Commented May 15, 2014 at 12:30
  • If the data shouldn't be interacted with, don't make it a select. Just display it. If you need a static value posted with the form, use a input type="hidden" or include it in the action route for the form. (Note: Both of those options do allow a savvy user to change the value. If the user must not change the value then keep it server-side.) Commented May 15, 2014 at 12:32
  • Hi Tintyethan, Yes....you are right. Commented May 15, 2014 at 13:20

4 Answers 4

1

Why don't you keep this disabled and add a <input type="hidden"> with the information you need to be posted?

Obviously this wasn't very clear so:

<script>

    var selectVal = $("#select_user > option[selected]").val();

    $("#select_post").val(selectVal);

</script>

<select disabled id="select_user">
    <option>Option 1</option>
    <option selected>Option 2</option>
    <option>Option 3</option>
</select>

<input type="hidden" name="select_post" id="select_post">

Now the user can see the select, but the value is replicated in the hidden input and then sent to the action file. Thankfully, since the select never changes, you don't have to do anything with .change()

Sign up to request clarification or add additional context in comments.

4 Comments

I need to display these values to user
A disabled input is still readable...
I know that disabled != hidden but can we post disbaled one ??
Edited the answer with example
0

There is no readonly attribute available for the element and disabling the field was not an option since we needed the value on the serverside. Some simple javascript did the job:

You can try this script

<select size=4 multiple="multiple" onchange="this.selectedIndex = 1">
  <option selected="selected">Volvo</option>
  <option selected="selected">Saab</option>
  <option  selected="selected">Mercedes</option>
  <option  selected="selected">Audi</option>
</select>

This will only do the job if you meant that the user should be able to see all values in the list and not be able to select a new value.

Demo

4 Comments

If you can downvote, then can comment too! aren't you?
Hey, it wasn't me .Infact i don't want to downvote any answer as, all are helping me!!.. Infact i voted just now . so now it is neutral.
@shrikanth I know, but my comment is for one who downvoted!
Thanks for your response .I wanted something like this... when the page gets loaded all values are selected and user should not be allowed to make any selection changes. But your demo default to index 1 as always
0

As far as I know, readonly doesn't exist for select boxes.

As for best practice, if you don't need the data to be changed, you shouldn't make it a form element. If you only make it a form element in order to get a certain display, use CSS to make it LOOK like a form element.

Plus, if data shouldn't be changed, it's safer not to send it with a form. Javascript can be disabled easily.

CSS

ul.fake_select{
        list-style-type:none;
        color:#000000;
        overflow-y:scroll;
        width:100px;
        height:auto;  /* If you have lots of options, put a fixed value */
        padding:0;
}
ul.fake_select li{
        margin:0;
        background:#3399FF;
        color:#FFFFFF;
}

HTML

<ul class="fake_select">
    <li>Volvo</li>
    <li>Saab</li>
    <li>Mercedes</li>
    <li>Audo</li>
</ul>

2 Comments

Thanks for the help. I'm creating listbox runtime using html embedded with php.So it will be great if you modify my code as given before
Sorry, I don't understand what you mean. Regardless of what language you use to output your html, it doesn't change the solution I gave you.
0
   <select size=4 multiple="multiple" onchange="this.selectedIndex=this.defaultIndex;">
  <option selected="selected">Volvo</option>
  <option selected="selected" >Saab</option>
  <option  selected="selected" >Mercedes</option>
  <option  selected="selected" >Audi</option>
</select>

Demo :-

http://jsfiddle.net/2gmKY/2/

5 Comments

Sorry Neel. It allows user to make selection. Thank you
I have updated @shrikanth
Neel ,Thanks for helping me out.. I checked fiddle submitted by you. first time all listbox values are selected . however when user clickon any values it selects first value only. My requirement is when user clicks on any of these values,original selected values shouldn't be changed .(meaning it should select all values as before)
sorry @shrikanth I am not getting you
Neel, Here is my observation. First time when the page gets loaded all the values are selected. When user clicks on any of these values , only first values gets selected. But I want to select all values as before

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.