943,948 Members | Top Members by Rank

Ad:
Jan 21st, 2008
0

un-disable dropdown box

Expand Post »
Hi I have some code here that fills a dropdown box from an array depending on what is selected in another dropdown box. One of them is disabled, and i would like to stop that once an option has been selected.

This is the Javascript:

javascript Syntax (Toggle Plain Text)
  1. function fillStates(opt){
  2. s=document.f.states.options
  3. s.length=stateNames[opt].length
  4. for(i=0;i<s.length;i++){
  5. s[i].value=stateNames[opt][i]
  6. s[i].text=stateNames[opt][i]
  7. }
  8. document.f.states.selectedIndex=0
  9. }

and the html
html Syntax (Toggle Plain Text)
  1. <form name="f">
  2. <select name=countries onChange="fillStates(this.options[this.selectedIndex].value)">
  3. <? echo $optionc; ?>
  4. </select>
  5. <select name=states disabled="disabled">
  6. <option>Choose a country first</option>
  7. </select>
  8. </form>

if anyone could give me a way to remove the disabled part on the second box once an option in the first box has been selected I would be grateful.

Thanks
Similar Threads
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
hooray is offline Offline
62 posts
since Jan 2008
Jan 21st, 2008
0

Re: un-disable dropdown box

<listbox>.disabled = false
Last edited by ShawnCplus; Jan 21st, 2008 at 4:59 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jan 21st, 2008
0

Re: un-disable dropdown box

document.getElementById('states').disabled=false;
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007
Jan 21st, 2008
0

Re: un-disable dropdown box

Click to Expand / Collapse  Quote originally posted by johnsquibb ...
document.getElementById('states').disabled=false;
That won't work. The selectbox doesn't have an ID, set the ID to states then do the above for it to work.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jan 21st, 2008
0

Re: un-disable dropdown box

thanks guys, this is how i fixed it:

javascript Syntax (Toggle Plain Text)
  1. if (opt=="none"){document.f.states.disabled=true}
  2. else {document.f.states.disabled=false}
Last edited by hooray; Jan 21st, 2008 at 8:08 pm.
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
hooray is offline Offline
62 posts
since Jan 2008
Jan 21st, 2008
0

Re: un-disable dropdown box

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
That won't work. The selectbox doesn't have an ID, set the ID to states then do the above for it to work.
yes shawnCplus is right! change name to id or just simply add id as in:
<select name="states" id="states">

Otherwise you would have to use the getElementsByName() method to get all the elements with the name "states".
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007
Jan 23rd, 2008
0

Re: un-disable dropdown box

> Otherwise you would have to use the getElementsByName() method to get all the elements
> with the name "states".
Or simply document.forms[0].elements["states"].disabled = false; which is the correct way of handling form elements.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Works in FF, not in IE7
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript help. Loop once!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC