Jump to content

Read Text from listbox?


A JM

Recommended Posts

I want to read the selected "text" from a <SELECT> on my form and not the "value" how would I do that?

 

All I can find is how to read the "value" and since my "value" and "text" are different the "text" is what I need.

 

Thanks.

 

A JM,

 

 

Link to comment
Share on other sites

Do you mean...

 

<SELECT>
<option value="somevalue">sometext</option>
</SELECT>

 

Oddly enough, I have something that allows you to switch stuff :-D Made it myself. Just give your select an id and change it for the script. Then, when the form is submitted, just have it switch the text and values and then let the form submit.

 

function shownamval() {
var selection = document.getElementById('mySelect');
var selectedIndex = selection.selectedIndex;
for (i=0;i<selection.length;i++)
{
var names = selection.options[i].innerHTML;
var values = selection.options[i].value;
selection.options[i].innerHTML = values;
selection.options[i].value = names;
}
}

 

If you only want the selected option to have the text/value switched, only:

function shownamval() {
var selection = document.getElementById('mySelect');
var selectedIndex = selection.selectedIndex;
var names = selection.options[selectedIndex].innerHTML;
var values = selection.options[selectedIndex].value;
selection.options[selectedIndex].innerHTML = values;
selection.options[selectedIndex].value = names;
}

 

Link to comment
Share on other sites

Oddly enough, I have something that allows you to switch stuff :-D Made it myself. Just give your select an id and change it for the script. Then, when the form is submitted, just have it switch the text and values and then let the form submit.

 

What is the point of that. If you want the "text" of the select field, then just make the value the same as the text. Trying to manipulate the values on submission is completely unnecessary and would fail for anyone without javascript enabled

Link to comment
Share on other sites

Oddly enough, I have something that allows you to switch stuff :-D Made it myself. Just give your select an id and change it for the script. Then, when the form is submitted, just have it switch the text and values and then let the form submit.

 

What is the point of that. If you want the "text" of the select field, then just make the value the same as the text. Trying to manipulate the values on submission is completely unnecessary and would fail for anyone without javascript enabled

 

I understand that, but I don't know exactly what he is doing (perhaps he cannot manipulate the values, but merely the text) so I just provide the poster with a way that will help him do what he wants instead of forcing him down the one path.

Link to comment
Share on other sites

"Thanks for the suggestion but the value and text must remain different." - why?

If you need the text then you might as well make the value and text the same.

What could you possibly be using the text for where the value isn't a more pertinent piece of information?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.