/    Sign up×
Community /Pin to ProfileBookmark

Calling a function within an option tag

Hello world!

I’m trying to figure out, but I can’t, how to call a JS function that’s defined in the header of my page within the OPTION tag of a SELECT tag.

What’s happening is that as the page loads I’m pulling the options together with a SQL statement and if the statement yields one record I want the form to select the value and run a JS that pre-populates other fields in the form.

When the list yields multiple names, then they just click and the JS runs with an onclick event handler. However, when the list yields one item, none of the mouse or keyboard event handlers would apply to get the JS to happen. I tried just popping it in as an “onload” within the OPTION tag, but that didn’t work either. Is there another way to do this?

I’m a bit stuck on where the JS is called because I’m interspersing with VBscript and SQL statements.

Thanks in advance!!!!!

Me ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@metatadSep 25.2009 — Try with onFocus...
Copy linkTweet thisAlerts:
@JMRKERSep 26.2009 — Events will not trigger within an <option> tag in MSIE.

They will trigger in FF.

For MSIE, you will need to put the action trigger in the <select> tag

and decide what to do bases upon the option value passed to the function.

Simple example:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Select Box Triggers&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Event triggers in FF but not MSIE
&lt;select id="SBox1"&gt;
&lt;option valu=""&gt;&lt;/option&gt;
&lt;option value="1" onclick="alert(this.value)"&gt;One&lt;/option&gt;
&lt;option value="2" onclick="alert(this.value)"&gt;Two&lt;/option&gt;
&lt;option value="3" onclick="alert(this.value)"&gt;Three&lt;/option&gt;
&lt;option value="4" onclick="alert(this.value)"&gt;Four&lt;/option&gt;
&lt;option value="5" onclick="alert(this.value)"&gt;Five&lt;/option&gt;
&lt;/select&gt;
&lt;p&gt;
Event triggers in FF and MSIE
&lt;select id="SBox2" onchange="alert(this.value)"&gt;
&lt;option valu=""&gt;&lt;/option&gt;
&lt;option value="1"&gt;One&lt;/option&gt;
&lt;option value="2"&gt;Two&lt;/option&gt;
&lt;option value="3"&gt;Three&lt;/option&gt;
&lt;option value="4"&gt;Four&lt;/option&gt;
&lt;option value="5"&gt;Five&lt;/option&gt;
&lt;/select&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @NatuScape spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...