/    Sign up×
Community /Pin to ProfileBookmark

Help with selected option

hi! I just joined this form and hope to find and answer to my question. Consider me as a javascript newbie ?. My question is i have this for form with couple options and a text box and what I want to do is when user selects the “none” textbox to be enabled and if user chooses “mysite” option textbox to be disabled so user can`t put his/her input. here is my code:

[code]
<form id=”form1″ method=”post” action=””>
<label>
<select name=”none” id=”noneid” >
<option value=”none”>None</option>
<option value=”mysite”>Mysite</option>
</select>
</label>
<label>
Another
<input type=”text” name=”another” id=”anotherid” />
</label>
</form>
[/code]

I think i need a javascript function to trigger by onchange event to disable or enable textbox depending on the user input. But i dont know how to write that function.

Thank You in advance for your help

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@cootheadAug 22.2010 — Hi there taocg,

and a warm welcome to these forums. ?

Try it like this...
[color=navy]
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta name="language" content="english"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;

&lt;title&gt;&lt;/title&gt;

&lt;style type="text/css"&gt;
.disabled {
background-color:#d4d0c8;
}
&lt;/style&gt;

&lt;script type="text/javascript"&gt;

function disableEnable(){

obj=document.getElementById('anotherid');

document.getElementById('noneid').onchange=function() {
this.value=='mysite'?
(obj.disabled=true,obj.className='disabled'):
(obj.disabled=false,obj.className='');
}
}

if(window.addEventListener){
window.addEventListener('load',disableEnable,false);
}

else {
if(window.attachEvent){
window.attachEvent('onload',disableEnable);
}
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;form id="form1" method="post" action="#"&gt;
&lt;div&gt;
&lt;label&gt;
&lt;select name="none" id="noneid" &gt;
&lt;option value="none"&gt;None&lt;/option&gt;
&lt;option value="mysite"&gt;Mysite&lt;/option&gt;
&lt;/select&gt;
&lt;/label&gt;
&lt;label&gt;
Another
&lt;input type="text" name="another" id="anotherid"&gt;
&lt;/label&gt;
&lt;/div&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
[/color]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@taocgauthorAug 22.2010 — Thank you for your help ?. This code solved my problem again Thank you very much.
Copy linkTweet thisAlerts:
@cootheadAug 22.2010 — [indent]No problem, you're very welcome. ?[/indent]
×

Success!

Help @taocg 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.15,
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,
)...