473,563 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2127
@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.select edIndex;
var correspondingVa lue =
document.forms["fakeform"].example[selInd].value;
var correspondingTe xt = document.forms["fakeform"].example[selInd].text;
// do something usefull with it

alert("selInd=" +selInd+"\ncorr espondingValue= "+corresponding Value+"\ncorres pondingText="+c orrespondingTex t);
}
</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************ *************** *************** @spamyourself.c om> 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.select edIndex;
var correspondingVa lue =
document.forms["fakeform"].example[selInd].value;
var correspondingTe xt = document.forms["fakeform"].example[selInd].text;
// do something usefull with it

alert("selInd=" +selInd+"\ncorr espondingValue= "+corresponding Value+"\ncorres pondingText="+c orrespondingTex t);
}
</script>

Regards,
Erwin Moller


Thanks!

Mar 22 '06 #4
@sh
Any ideas?
Mar 22 '06 #5
In article <DM************ *************** ***@bt.com>,
"@sh" <sp**@spam.co m> 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**********@d ante.org.uk> wrote in message
news:ti******** *************** *********@indiv idual.net...
In article <DM************ *************** ***@bt.com>,
"@sh" <sp**@spam.co m> 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.se lectedIndex = k;
}

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

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

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

Similar topics

2
1527
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
15140
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
16617
by: Tjerk Wolterink | last post by:
Hello, suppose i have a dom like this: <a> - <b> - <b> - <d> - </b> - <c>
5
3344
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 columns when some cells are empty. Is there a way to use the ss:Index to account for the missing <Cell elements? Thank you for any suggestions. ...
1
2556
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. Below is my code. here strSelectSQL value is strSelectSQL = "Select emp.Empno, emp.FirstName, emp.LastName, emp.DB,...
2
3639
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 /////////////////////////////////////////////////////////////////////////////////////// <form action="whatever.php" method="post"> <select name="zip_code"...
0
1753
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"> <xsl:template match="/"> <xsl:choose> <xsl:when test="local-name(fp:AC_FlightPassesRS/fp:Success)='Success'">
4
2887
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 the text in the field to be automatically selected. How can I do this?
4
2087
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 value="1" text="g"
7
2032
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 vlan_name") dim v_lan do while not rs.eof%> <option value="<%=rs("vlan_name")%>"><%=rs("vlan_name")%></option> <% rs.movenext loop%>...
0
7664
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.