473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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!
Mar 22 '06 #1
16 2108
@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="+cor respondingValue+"\ncorrespondingText="+correspondi ngText);
}
</script>

Regards,
Erwin Moller


Thanks!


Mar 22 '06 #2
Erwin Moller wrote:

Correction:
<select name="example" onClick="getVal();">


By the way: onClick is not OK.
It should be onChange().
Mar 22 '06 #3
@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"
<si******************************************@spam yourself.com> wrote in
message news:44***********************@news.xs4all.nl...
@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="+cor respondingValue+"\ncorrespondingText="+correspondi ngText);
}
</script>

Regards,
Erwin Moller


Thanks!

Mar 22 '06 #4
@sh
Any ideas?
Mar 22 '06 #5
In article <DM******************************@bt.com>,
"@sh" <sp**@spam.com> wrote:
Any ideas?


About what?

-- tim
Mar 22 '06 #6
@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!
"Tim Streater" <ti**********@dante.org.uk> wrote in message
news:ti********************************@individual .net...
In article <DM******************************@bt.com>,
"@sh" <sp**@spam.com> wrote:
Any ideas?


About what?

-- tim

Mar 22 '06 #7
@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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 22 '06 #8
Erwin Moller wrote:
Erwin Moller wrote:
<select name="example" onClick="getVal();">


By the way: onClick is not OK.
It should be onChange().


That depends.
PointedEars
Mar 22 '06 #9
Thomas 'PointedEars' Lahn said the following on 3/22/2006 6:08 PM:
Erwin Moller wrote:
Erwin Moller wrote:
<select name="example" onClick="getVal();">

By the way: onClick is not OK.
It should be onChange().


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 23 '06 #10
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;
}

:-)

--
Rob
Mar 23 '06 #11
RobG wrote:
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
Mar 23 '06 #12
JRS: In article <_J********************@comcast.com>, dated Wed, 22 Mar
2006 18:53:38 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :

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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 24 '06 #13
Dr John Stockton said the following on 3/24/2006 8:36 AM:
JRS: In article <_J********************@comcast.com>, dated Wed, 22 Mar
2006 18:53:38 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
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.


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Answer:It destroys the order of the conversation
Mar 25 '06 #14
test

Mar 25 '06 #15
sh******@gmail.com said the following on 3/24/2006 10:32 PM:
test


fail.

alt.test is that way over there ----->

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 25 '06 #16
sh******@gmail.com wrote:
test


Test failed.
F'up2 set

PointedEars
Mar 25 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: thomas | last post by:
Hello all Im having problems with selecting subelements from a dynamic created RTF converted to a node-set: i have hardcodet this example: the xml structure: <xsl:variable name="test">
2
by: Mike Kamermans | last post by:
I'm having some trouble using text() in an xsl:value-of xpath. I have the following xml: .... <graphemes> <grapheme>1</grapheme> <grapheme>2</grapheme> <grapheme>3</grapheme> </graphemes>...
9
by: Tjerk Wolterink | last post by:
Hello, suppose i have a dom like this: <a> - <b> - <b> - <d> - </b> - <c>
5
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting...
1
by: Ramesh | last post by:
hi, I am selecting fields from three table for manupulating data and i want to display total number of records selected. But i am always getting -1 value, eventhough 1000 of records are selected....
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
0
by: zipzap | last post by:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fp="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fp"> ...
4
by: Paul J. Lucas | last post by:
I have a simple form that contains an INPUT element that has an initial value. <form name="Form"> <input name="Number" type="text" value="Hello"> </form> When the form loads, I want all of...
4
by: darrel | last post by:
I have a DDL list along these lines: item value="1" text="a" item value="2" text="b" item value="3" text="c" item value="2" text="d" item value="2" text="e" item value="1" text="f" item...
7
vikas251074
by: vikas251074 | last post by:
Can I assign value to cookies immediately after selecting a value from list? <select name="vlan_name" style="width:150px "> <%set rs = conn.execute("select vlan_name from vlan_master order by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.