Jump to content

[SOLVED] need some help!!


pikemsu28

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/44010-solved-need-some-help/
Share on other sites

--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;
    }
}

Link to comment
https://forums.phpfreaks.com/topic/44010-solved-need-some-help/#findComment-214548
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/44010-solved-need-some-help/#findComment-215298
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/44010-solved-need-some-help/#findComment-215691
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.