Using Javascript to select an entry in a drop down box

A

Andy Lee

Hi

I have a dropdown box containing about 10 values.

I would like to create a separate href on my page that when clicked will
make the drop down box go to a specific value. I do not want to have to
refresh the page to do this. Is it possible to have that sort of control
over a drop down box. ?

Andy
 
K

kaeli

Hi

I have a dropdown box containing about 10 values.

I would like to create a separate href on my page that when clicked will
make the drop down box go to a specific value. I do not want to have to
refresh the page to do this. Is it possible to have that sort of control
over a drop down box. ?

Yes, but using an href would be a bad idea for an internet application. I'd
use a button, myself, styled to look like a text link if such was the
requirement.

If you decide to use an anchor, it would be
<a href="someNoJSPage.html" onClick="choose();return false;">link</a>

function choose()
{
// change the names and selectedIndex appropriately
// both versions supplied for cross-browser
document.forms["myformname"].elements["myselect"].selectedIndex = 2;
document.forms["myformname"].elements["myselect"].options[2].selected =
true;
}

You can change that to accept a number param or whatever.

--
 
M

Matt Kruse

kaeli said:
Yes, but using an href would be a bad idea for an internet
application.

Not necessarily. You could use javascript to document.write the <a>, since
the link would only be relevant if js was enabled. Then you wouldn't need to
worry about onclick not firing.
 
K

kaeli

Not necessarily. You could use javascript to document.write the <a>, since
the link would only be relevant if js was enabled. Then you wouldn't need to
worry about onclick not firing.

Yes, but then people without script couldn't use the page. Which, IMNSHO, is
a bad thing for many reasons.

--
 
M

Matt Kruse

kaeli said:
Yes, but then people without script couldn't use the page. Which,
IMNSHO, is a bad thing for many reasons.

That's not true - they could still use the page just fine. They just
couldn't trigger the functionality of selecting a certain value in the
select list. Since this functionality requires javascript, it makes sense to
only write the control if it can be used.

This is a better approach than pointing the href to
"javascript_required_notice.html" or something, if there's a way to hide the
link entirely if it will serve the user no purpose.
 
K

kaeli

That's not true - they could still use the page just fine. They just
couldn't trigger the functionality of selecting a certain value in the
select list.


See, to me, that means they can't use the page. They can SEE it. They can't
USE it, since part of it is then broken.

Since I don't know what the page actually IS or what it is meant to do, the
fact that a control is broken may or may not break the entire thing. But at
least part of it doesn't work the same for people with no script as it does
for people with it - therefore, it is not fully functional (usable) for non-
JS users.
For all I know, it's actually a personal page or an intranet page and
everyone has script and it's a moot point. The OP didn't say.

*shrugs*

--
--
~kaeli~
Is it true that cannibals don't eat clowns because they
taste funny?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top