/    Sign up×
Community /Pin to ProfileBookmark

Help in function!

I’m trying to create a funnction which would check too ssee if a variable was set, and output certain html code if it was, and output soome other html code if it wasn’t
here’s my html code

[code=html]
<select name=”country” onChange=”checkCountry()”> ……..</select>
[/code]

heres the javascript function
function checkCountry() {
var ctry = document.form1.country[document.form1.country.selectedIndex].value;
if (ctry == “USA”) {
document.write(<p><label>State</label><input type=text name=state></p>);
} else {
document.write(<p><label>Country</label><input type=text name=country></p>);
}
}
Thanks!
(is there another easier way to do this?)

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@deep_dhyaniAug 08.2005 — use this code:


<html>

<head>

<SCRIPT Language="javascript">

</SCRIPT>

</head>

<body>

<form name="f1">

<label id='name_lbl'>State</label><input type="text" name="name"><br>

<Select

onChange="if(this.value=='USA'){document.getElementById('name_lbl').innerText='Country';}else{docum

ent.getElementById('name_lbl').innerText='State';}">

<option value="USA">USA</option>

<option value="AUS">AUS</option>

</select>

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@lukeurtnowskiauthorAug 08.2005 — OK, I see what that does, 1 question, when the label tuurns to state, how do I also change the input box to a select box?

Thanks
Copy linkTweet thisAlerts:
@deep_dhyaniAug 08.2005 — here is code:

<html>

<head>

<SCRIPT Language="javascript">

function changeElement(){

var val=document.f1.sel.options[document.f1.sel.selectedIndex].value;

if(val=="USA"){

document.getElementById("name_lbl").innerHTML="<select><option>Cities</option></select>";

}else{

document.getElementById("name_lbl").innerHTML="<input name='id' type='text' /><br>";

}
}

</SCRIPT>

</head>

<body>

<form name="f1">

<label>State</label><div id='name_lbl'><input name='id' type="text" /><br></div>

<Select name="sel" onChange="changeElement()">

<option value="USA">USA</option>

<option value="AUS">AUS</option>

</select>

</form>

</body>

</html>
×

Success!

Help @lukeurtnowski 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.16,
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,
)...