Selecting a SELECT value BY value?

@

@sh

Probably very simple, but I have a SELECT box The value of each option in
the Select box will be a number, for instance...

<option value="5">A test entry</option>

Therefore, how do I, via an onClick, change the selected option of a Select
via the Value of the select only? I can't use the 'selectedIndex' because it
doesn't relate to the value of the option.

Thanks!
 
E

Erwin Moller

@sh wrote:


Hi
Probably very simple, but I have a SELECT box The value of each option in
the Select box will be a number, for instance...

<option value="5">A test entry</option>

Therefore, how do I, via an onClick, change the selected option of a
Select via the Value of the select only? I can't use the 'selectedIndex'
because it doesn't relate to the value of the option.

Yes it does relate to the value.
It is actually POINTING to the selected index, which is more usefull than
the value, since the same value can be found for different options!
So use it to point to the right OPTION in the selectbox, and retrieve the
value or text.

Example:

<form name="fakeform">
<select name="example" onClick="getVal();">
<option value="1">One
<option value="2">Two
<option value="3">Three
<option value="1">Also One
</select>
</form>

<script type="text/javascript">
function getVal(){
// get the index of the selected one
var selInd = document.forms["fakeform"].example.selectedIndex;
var correspondingValue =
document.forms["fakeform"].example[selInd].value;
var correspondingText = document.forms["fakeform"].example[selInd].text;
// do something usefull with it

alert("selInd="+selInd+"\ncorrespondingValue="+correspondingValue+"\ncorrespondingText="+correspondingText);
}
</script>

Regards,
Erwin Moller
 
@

@sh

Thanks for your reply, I think I understand what you mean, although I'm
trying to set the selected option in the SELECT via an onClick from another
element on the page? I think you've misunderstood my question?


"Erwin Moller"
@sh wrote:


Hi
Probably very simple, but I have a SELECT box The value of each option in
the Select box will be a number, for instance...

<option value="5">A test entry</option>

Therefore, how do I, via an onClick, change the selected option of a
Select via the Value of the select only? I can't use the 'selectedIndex'
because it doesn't relate to the value of the option.

Yes it does relate to the value.
It is actually POINTING to the selected index, which is more usefull than
the value, since the same value can be found for different options!
So use it to point to the right OPTION in the selectbox, and retrieve the
value or text.

Example:

<form name="fakeform">
<select name="example" onClick="getVal();">
<option value="1">One
<option value="2">Two
<option value="3">Three
<option value="1">Also One
</select>
</form>

<script type="text/javascript">
function getVal(){
// get the index of the selected one
var selInd = document.forms["fakeform"].example.selectedIndex;
var correspondingValue =
document.forms["fakeform"].example[selInd].value;
var correspondingText = document.forms["fakeform"].example[selInd].text;
// do something usefull with it

alert("selInd="+selInd+"\ncorrespondingValue="+correspondingValue+"\ncorrespondingText="+correspondingText);
}
</script>

Regards,
Erwin Moller

 
@

@sh

About my dilemma! I'm a bit stuck and have been toying all afternoon but
can't quite solve it - I think the reply from Erwin misunderstood my
question, either that or I misunderstood how to interpret his reply?

I basically need a way to, via onClick from another page element, set a
Select box to an option knowing only the 'value' of the Option in question?

Thanks!
 
R

Randy Webb

@sh said the following on 3/22/2006 11:17 AM:

Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
About my dilemma! I'm a bit stuck and have been toying all afternoon but
can't quite solve it - I think the reply from Erwin misunderstood my
question, either that or I misunderstood how to interpret his reply?

I basically need a way to, via onClick from another page element, set a
Select box to an option knowing only the 'value' of the Option in question?

You loop through the select options and when you find the one that has
the value you want, you set the selectedIndex to the one that has that
value. And, to get to the value, you have to know the selectedIndex.


function setSelectObject(val){
var selectObject = document.forms['myForm'].elements['mySelect'];
var k=0;
while (selectObject[k].value != val)
{
k++
}
selectObject.selectedIndex = k;
}

It will pick up the first option that has the value that you pass it.
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 3/22/2006 6:08 PM:
That depends.

No it doesn't. Onclick on a select is like onblur of any other input
element. You don't use it to change/validate things because then you are
repeatedly running code when it isn't needed. onChange will only fire
when it's changed, meaning your code only runs when it needs to.
 
R

RobG

Randy Webb said on 23/03/2006 6:50 AM AEST:
[...]
function setSelectObject(val){
var selectObject = document.forms['myForm'].elements['mySelect'];
var k=0;
while (selectObject[k].value != val)
{
k++
}
selectObject.selectedIndex = k;
}

It will pick up the first option that has the value that you pass it.


Or to get the last one...

function setSelectObject(val)
{
var selectObject = document.forms['myForm'].elements['mySelect'];
var k = selectObject.length;
while (selectObject[--k].value != val){}
selectObject.selectedIndex = k;
}

:)
 
E

Erwin Moller

RobG said:
Randy Webb said on 23/03/2006 6:50 AM AEST:
[...]
function setSelectObject(val){
var selectObject = document.forms['myForm'].elements['mySelect'];
var k=0;
while (selectObject[k].value != val)
{
k++
}
selectObject.selectedIndex = k;
}

It will pick up the first option that has the value that you pass it.


Or to get the last one...

function setSelectObject(val)
{
var selectObject = document.forms['myForm'].elements['mySelect'];
var k = selectObject.length;
while (selectObject[--k].value != val){}
selectObject.selectedIndex = k;
}

:)

Hi @sh,

Erwin has nothing more to add to that. :)
Go use the code.

And I indeed misinterpreted your original question, allthough I prefer the
point of view your question was very vague. ;-)

Regards,
Erwin Moller
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 22 Mar
2006 18:53:38 remote, seen in Randy Webb
No it doesn't. Onclick on a select is like onblur of any other input
element. You don't use it to change/validate things because then you are
repeatedly running code when it isn't needed. onChange will only fire
when it's changed, meaning your code only runs when it needs to.

The programmer must remember that visiting a select and not changing it
can amount to making a selection ("Now I've seen the other options, I
want what was already showing"). So the selection may need to be
validated by something other than its own onChange, perhaps by a "Use
it/them" button. It may be appropriate to process the initial state in
page load, as if it had been selected by the user rather than the
programmer.
 
R

Randy Webb

Dr John Stockton said the following on 3/24/2006 8:36 AM:
JRS: In article <[email protected]>, dated Wed, 22 Mar
2006 18:53:38 remote, seen in Randy Webb


The programmer must remember that visiting a select and not changing it
can amount to making a selection ("Now I've seen the other options, I
want what was already showing"). So the selection may need to be
validated by something other than its own onChange, perhaps by a "Use
it/them" button. It may be appropriate to process the initial state in
page load, as if it had been selected by the user rather than the
programmer.

I am not sure I agree with that though. While it is a possibly valid
approach, most Select lists have a first option of something to the
effect of "Choose an Option" and then you validate against that. If it
isn't changed, then you tell them to choose one. And the very scenario
you describe is part of what started that trend.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top