pikemsu28 Posted March 23, 2007 Share Posted March 23, 2007 i found some code that will enable a select menu if a check box is checked and it is exactly what i need. except that it is designed for only one check box and one select menu. how can the following code be configured to pass the elements name so that i can use it for any check box that needs to enable a select menu? <html> <head> <script language="javascript"> function enable() { if (document.forms["form1"].elements["check1"].checked == true) { document.forms["form1"].elements["select1"].disabled = false; } else { document.forms["form1"].elements["select1"].disabled = true; } } </script> </head> <body> <form name="form1"> <input type="checkbox" name="check1" onClick="enable()"/><label for="check1">Check box Enables Select Box</label> <select name="select1" disabled="disabled"> <option selected="selected" value="">Select</option> <option value="option1">option1</option> <option value="option2">option2</option> <option value="option3">option3</option> </select> </form> </body> </html> i dont know very much about javascript but it looks like to me that the element's name needs to be passed to a variable in the function that in turn populates this part of the function: elements["element_name"]. I'm just not sure on how to do it. Some guidance would be greatly appreciated. Thanks, Jason Quote Link to comment Share on other sites More sharing options...
mainewoods Posted March 25, 2007 Share Posted March 25, 2007 --pass the id of the select control of interest and the 'this' as parameters in the onclick: <input type="checkbox" name="check1" onClick="enable(this, 'select1')"/> then just code your enable() with parameters like this: function enable(radiocontrol, forselectID) { if (radiocontrol.checked == true) { document.forms["form1"].elements[forselectID].disabled = false; } else { document.forms["form1"].elements[forselectID].disabled = true; } } Quote Link to comment Share on other sites More sharing options...
pikemsu28 Posted March 26, 2007 Author Share Posted March 26, 2007 nice, thanks mainewoods. can this work for arrays as well? say i have three check boxes with the name='semester[]' and three select menus with name='year[]'. can i pass the array to the function? and if so, is there a way that i can get the first check box (semester[]) to correspond with the first select menu (year[])? if this is unclear i can explain what i'm trying to accomplish thanks again. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted March 26, 2007 Share Posted March 26, 2007 Give those controls an id="" and then pass the id of interest in the function call. Each must have a different id, no dup ids allowed in a html page. To access the select control values, use this dom reference for the select: http://www.w3schools.com/htmldom/dom_obj_select.asp You can access the properties/methods like this: //to retrieve property var selectedindex = document.getElementById(thepassedID).selectedIndex; //to set property document.getElementById(thepassedID).selectedIndex = somenumber; Quote Link to comment Share on other sites More sharing options...
pikemsu28 Posted March 27, 2007 Author Share Posted March 27, 2007 Sweet, thanks!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.