473,407 Members | 2,598 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,407 software developers and data experts.

How to access name value pairs from a <select>?

Say I have:

<select>
<option value='a' > First
<option value='b' > Second
<option value='c' > Third
</select>

Is there a way I can access the values First, Second and Third from
an array ? I want to write a javascript function that can
automatically select one option based on a regular expression match
of First Second or Third.

Apr 5 '06 #1
3 2959
ve*****@yahoo.com said the following on 4/5/2006 11:45 AM:
Say I have:

<select>
<option value='a' > First
<option value='b' > Second
<option value='c' > Third
</select>

Is there a way I can access the values First, Second and Third from
an array ? I want to write a javascript function that can
automatically select one option based on a regular expression match
of First Second or Third.


Get the .text property of the select. It will give you the First, Second
and Third that you are hunting.

So, loop through the select list checking it's .text property and then
when you find it, set the selectedIndex of the select list to the one
that corresponds to the .text property you want.

function setSelect(txt){
var sel = document.myForm.mySelect;
var selLength = sel.length;
var found = false;
for (var i=0;i<selLength;i++)
{
if (sel.options[i].text.toLowerCase() == txt.toLowerCase())
{
sel.options.selectedIndex = i;
found = true;
break;
}
}
if (!found){alert('That entry not found')}
}

With this HTML:

<form name="myForm">
<select name="mySelect">
<option value='a'>First
<option value='b'>Second
<option value='c'>Third
<option value='d'>Fourth
<option value='e'>Fifth
<option value='f'>Sixth
<option value='g'>Seventh
<option value='h'>Eighth
<option value='i'>Ninth
<option value='j'>Tenth
<option value='k'>Eleventh
<option value='l'>Twelfth
<option value='m'>Thirteenth
</select>
<input type="text" onchange="setSelect(this.value)">
</form>

Simply call setSelect with the text you want searched for.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 5 '06 #2
ve*****@yahoo.com wrote:
Say I have:

<select>
<option value='a' > First
<option value='b' > Second
<option value='c' > Third
</select>

Is there a way I can access the values First, Second and Third from
an array ?


No, but you can read it from a collection. RTFFAQ.
PointedEars
Apr 6 '06 #3
Thomas 'PointedEars' Lahn said the following on 4/5/2006 10:21 PM:
ve*****@yahoo.com wrote:
Say I have:

<select>
<option value='a' > First
<option value='b' > Second
<option value='c' > Third
</select>

Is there a way I can access the values First, Second and Third from
an array ?
No,


Sure you can.
but you can read it from a collection.
You can get it that way also.
RTFFAQ.
As should you. And, read posted code before you babble your BS.
PointedEars


Now, is that a signature or not?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 6 '06 #4

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

Similar topics

1
by: Raghuram Banda | last post by:
Hi All, Can any one help me, how to get the selected value from <Select> tag using DOM I tried with document.getElementById("selectTagId").getAttribute("value") it's working fine with IE but...
1
by: Ang Talunin | last post by:
Hey, I wondering if it's possible to retrieve all the <option>-fields from a <select> when posting a <form> to a php file Example: I've got a form like this: <form action = phpfile.php...
4
by: joiv | last post by:
I'm making a <select></select> with lots of <option></option>. It contains all possible options. Because of the length of the list, I also have an <input type="text">. This is what I wish to do:...
5
by: Brian Foley | last post by:
Hello, I am used to using the label tag with check boxes and radio buttons in html forms. This allows me to click on the text of the label to activate/deactivate the check box / button, rather...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
4
by: Man-wai Chang | last post by:
-- iTech Consulting Co., Ltd. Expert of ePOS solutions Website: http://www.itech.com.hk (IE only) Tel: (852)2325 3883 Fax: (852)2325 8288
2
by: msg2ajay | last post by:
hi all, I have a problem with <html:select> i am getting a value from the database but it is not setting to my <select value>. Can any bady tellme what is the problem. My code is given...
14
mikek12004
by: mikek12004 | last post by:
In a form I have 5 elements (e.g. pictures) and I wish for the user to be able to set the order of appearance. For this I have for each picture a select box (names select1 to select5) with "please...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.