/    Sign up×
Community /Pin to ProfileBookmark

list menu to change text field

I’ve done it before, but now I’m bumbling.

Need to change text field based on option selected in list menu.

What have I done wrong? Please, need quick answer!

[QUOTE]

<script language=”JavaScript” type=”text/javascript”>
function whichTest() {
if(document.getElementById(‘rank’).option.selected.value = “Private” || “PFC” || “LCpl” || “Cpl” || “Sgt” || “GS01” || “GS02” || “GS03” || “GS04” || “GS05” || “GS06” || “GS07” || “GS08”)
{
document.getElementById(‘whichTest’).value = “EmpTest”;
} else {
document.getElementById(‘whichTest’).value = “SupvTest”;
}
}

</script>

[/QUOTE]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzOct 25.2007 — 1) It will only check the first one.

I would probably use a regular expression for this.

2) "=" is an assignment operator. "==" is the equality comparison operator.

3) I think you may be accessing the value of the form control incorrectly.

4) The language attribute of the <script> element is deprecated, so don't use it.

Try this:
if(/^(Private|PFC|LCpl|Cpl|Sgt|GS0[1-8])$/i.test(document.getElementById('rank').value))
Copy linkTweet thisAlerts:
@MikeMediaauthorOct 25.2007 — Sorry Kravvitz:

I am not grasping the logic. I've dropped your line to no effect.

&lt;script language="JavaScript" type="text/javascript"&gt;
function whichTest() {
if(/^(Private|PFC|LCpl|Cpl|Sgt|GS0[1-8])$/i.test(document.getElementById('rank').value))
{
document.getElementById('whichTest').value = "EmpTest";
} else {
document.getElementById('whichTest').value = "SupvTest";
}
} <br/>
&lt;/script&gt;
Copy linkTweet thisAlerts:
@MikeMediaauthorOct 25.2007 — Additional:

&lt;select name="rank" id="rank" onSelect="javasript:whichTest();"&gt;
&lt;option value="---"&gt;&lt;/option&gt;
&lt;option value="Private"&gt;Private&lt;/option&gt;
&lt;option value="PFC"&gt;PFC&lt;/option&gt;
&lt;option value="LCpl"&gt;LCpl&lt;/option&gt;
&lt;option value="Cpl"&gt;Cpl&lt;/option&gt;"&gt;


Etc.
Copy linkTweet thisAlerts:
@KravvitzOct 25.2007 — 1) onSelect="javasript:whichTest();"
should be
onchange="whichTest();"
2) It's best to avoid using inline event handlers whenever possible. I suggest you read up on [url=http://www.dynamicsitesolutions.com/javascript/best-practices/#unobtrusive_javascript]Unobtrusive JavaScript and other JavaScript Best Practices[/url].
Copy linkTweet thisAlerts:
@MikeMediaauthorOct 25.2007 — What th' heck am I doin' wrong?!?

Revised script in order to keep it REAL SIMPLE. Still, Nada!
&lt;head&gt;
&lt;script language="JavaScript" type="text/javascript"&gt;
function whichTest() {
var rank = document.form1.getElementById('rank').option.selected.value
if(rank == "Private")
{
alert("OK!");
}
} <br/>
&lt;/script&gt; <br/>
&lt;/head&gt;
&lt;body&gt;
&lt;form id="form1" name="form1" method="post" action=""&gt;
&lt;p&gt;
&lt;select name="dropper" id="dropper" onChange="whichTest()"&gt;
&lt;option value="null" selected="selected"&gt;&lt;/option&gt;
&lt;option value="you"&gt;you&lt;/option&gt;
&lt;option value="me"&gt;me&lt;/option&gt;
&lt;option value="them"&gt;them&lt;/option&gt;
&lt;option value="Private"&gt;Private&lt;/option&gt;
&lt;/select&gt;
&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@KravvitzOct 25.2007 — Like I said before, you're accessing the value property incorrectly.
var rank = document.form1.getElementById('rank').option.selected.value
should be
var rank = document.getElementById('rank').value;
OR
var rank = document.getElementById('rank')
rank = rank.options[rank.selectedIndex].value;
Copy linkTweet thisAlerts:
@MikeMediaauthorOct 27.2007 — Thank you.

Your assist was exactly what I needed.
×

Success!

Help @MikeMedia 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...