473,566 Members | 3,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

selecting an option from a select

Hi!

Since the first questions I asked here, I continue to drop by and read
some topics here and there, I like the replies very much.

I have today another question, I don't seem to find it on the FAQ
(please point me if its there):

I have two selects, one written 'directly on the page' and a second
written dinamically by JavaScript. (Actually, both are written by php
using data from a DB).

One is dependable of the other, and I am using the code of DevArticles
to make them depend:
http://tinyurl.com/df6vf

The Question: Now, I want to select the selected option of both inputs,
after the page is loaded.
How can I make the right option selected, based on the value of it?

I know the id and the value of each <OPTION>, but I do not know the
position of the <OPTION> inside the <SELECT>. So I guess I can not use
something like document.form.s electinput.opti ons[1].selected, unless I
iterate through all the options. If this is the way to do it, how is
the iteration to be done?

I hope you can help me on this, and thank you for all the times you
helped me on the past!

SGershon

Jul 23 '05 #1
5 10253
Hi again!
Maybe someone can point me why this wont work to solve the above
problem/question?

function setDefault()
{
for (var i=0 ; i<document.theF orm.theSelect.l ength ; i++)
{
if (document.theFo rm.theSelect.op tions[i].value == optVal)
{
document.theFor m.theSelect.opt ions[i].selected=true
}
}
}

Thanks!
SGershon

Jul 23 '05 #2
SGershon wrote:
Hi again!
Maybe someone can point me why this wont work to solve the above
problem/question?

function setDefault()
{
for (var i=0 ; i<document.theF orm.theSelect.l ength ; i++)
{
if (document.theFo rm.theSelect.op tions[i].value == optVal)
{
document.theFor m.theSelect.opt ions[i].selected=true
}
}
}

Thanks!
SGershon


Probably because: you're not calling it onload.

function setDefault(val)
{
var f, s, i = 0;
if ((f = document.theFor m)
&& (s = f.elements.theS elect))
{
for (var l = s.options.lengt h; i < l; i++)
{
if (s.options[i].value == val)
{
s.options[i].selected = true;
return;
}
}
}
}

window.onload = function()
{
setDefault('foo ');
}

Jul 23 '05 #3
No, it was some stupid typo on the code on the page.

Thanks for the hint, RobB... I am calling it on the end of the page,
inside a <script></script> tag... I guess is more clean/elegant to use
the onLoad event. What are the practical differences?

Jul 23 '05 #4
SGershon wrote:
No, it was some stupid typo on the code on the page.

Thanks for the hint, RobB... I am calling it on the end of the page,
inside a <script></script> tag... I guess is more clean/elegant to use the onLoad event. What are the practical differences?


Helps you keep your 'behavioral layer' separate from that other
rubbish:

http://www.alistapart.com/articles/scripttriggers/

Also, manipulating elements onload gives them an extra bit of time to
'set up', the absence of which is sometimes a source of some
verdrießlichkei t, if you know what I mean. Just remember,
window.onload is an object property & will be over-written if you
assign to it more than once...unless, of course, you use W3C DOM
addEventListene r() or MSIE attachEvent(), or 'bundle' multiple handlers
in a wrapper function.

Jul 23 '05 #5
SGershon wrote:
No, it was some stupid typo on the code on the page.

Thanks for the hint, RobB... I am calling it on the end of the page,
inside a <script></script> tag... I guess is more clean/elegant to use
the onLoad event. What are the practical differences?


The other side of the coin is that using onload with a heavy page can
cause an unacceptable lag in a script executing where visible page
elements are being modified.

Say you have something small to do a the top of the page that you'd
prefer happened without being noticed. Inserting a function just
after the element concerned can allow it to happen much sooner (and
less perceptibly) than if the browser waits for onload.

But that would be the exception rather than the rule.

--
Rob
Jul 23 '05 #6

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

Similar topics

5
2056
by: Lukelrc | last post by:
Hi, I have a dynamically created listbox. I'm trying to get one of the options selected according to a passed value. This is what i have: <select name="txtTheme" id="txtTheme"> <option>Select Theme</option> <% Dim PassedTheme
2
2059
by: mike | last post by:
I need to be able to select a specific option in a select list. This is what I have: <select name="mysel"> <option value=""></option> <option value="1">First</option> <option value="2" selected>First</option> <option value="3">First</option> </select> But I want to select the first option. Here is my code:
1
3602
by: jzhang29 | last post by:
I have a JSP page and it contains a dropdown list called Office. What I try to do is: When I select different office from this list, the information of office (address, phone,etc) will be populated in same JSP page. I have a java bean called officeBean that contains all the office information.
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"...
16
2128
by: @sh | last post by:
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...
1
2127
by: zorro | last post by:
and does testing merely mean running it 100 000 times and clocking it? // using preg_replace : $s = ' <select name="color"> <option value="">choose <option value="red">red <option value="green">green
1
2022
by: =?ISO-8859-1?Q?Une_B=E9vue?= | last post by:
my xml fragment : <select id='aId'> <option>option 1</option> <option>option 2</option> <option>---?---</option> <option>option n + 1</option> <option>option n + 2</option>
3
7149
by: vimal | last post by:
hi all, i am still a learner in javascript i have a select tag with options as follows: <select> <option py:for="mb in range(32,100)">${mb}</option> </select>
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%>...
2
1526
by: .nLL | last post by:
got below script, works fine with IE but fails on opera. my js knowledge is very limited and can't find whats wrong with it. ---------------------------------------------- function selectMatching(x) { var S = document.getElementById("mymonth"); var L = S.options.length; for (var i=0; i <= L-1; i++) {
0
7673
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
7584
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
7893
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. ...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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...
0
3643
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
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.