473,698 Members | 2,076 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
16 2143
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.se lectedIndex = 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.le ngth;
while (selectObject[--k].value != val){}
selectObject.se lectedIndex = 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.se lectedIndex = 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.le ngth;
while (selectObject[--k].value != val){}
selectObject.se lectedIndex = 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************ ********@comcas t.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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************ ********@comcas t.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.javas cript 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.javas cript 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
1534
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
15143
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
16654
by: Tjerk Wolterink | last post by:
Hello, suppose i have a dom like this: <a> - <b> - <b> - <d> - </b> - <c>
5
3356
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
2562
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, emp.DOJ,emp.Grade,emp.yearofexperience,
2
3656
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" onchange="makeRequest('getCity.php?state='+this.form.zip_code.options.value)" multiple="multiple" size="20">
0
1759
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
2891
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
2096
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
2036
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%> </select>
0
8603
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1999
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.