Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Applying two functions to one select box 1

Status
Not open for further replies.

MikeM2468

IS-IT--Management
Apr 5, 2011
100
US
I have a form with several select boxes. On the last box, I need two things to happen: First, when an option is selected, the Submit button gets enabled (disabled by default). Second is that if one specific option is selected, a hidden field is shown.

I had this working perfectly using onChange for the select to enable the submit button and an onClick on the option to show the hidden field. Later I found that the onClick doesn't work in IE.

Now I've created a function that will show the hidden field if the correct option is selected, but it runs onLoad in the body tag. When that's in there, the onChange function doesn't fire. If I add the hidden field function to the onChange along with the show submit function, the show submit function works, but the show hidden field function only works if I select the desired option, select another option, then select the desired option again. This last problem seems like it should be easy to fix but I don't know how.

Here are the functions I'm using:
Code:
function enable_submit(obj) {
  var sb = document.getElementById("Submit");
  sb.disabled = false;
}
function show_comment() {
  var eSelect = document.getElementById('f2');
  var optComment = document.getElementById('more');
  eSelect.onchange = function show_comment() {
   if(eSelect.selectedIndex === 1) {
     optComment.style.display = 'block';
   } else {
     optComment.style.display = 'none';
   }
  }
}

And here is the form:
Code:
<body onLoad="show_comment();">
<form action="form.php" method="post">
Field 1:&nbsp;<select name="f1" id="f1">
	<option value="">Please select:</option>
	<option value='1'>1</option>
	<option value='2'>2</option>
</select><br />
Field 2:&nbsp;<select name="f2" id="f2" onChange="enable_submit(this);">
	<option value="">Please select:</option>
	<option value='1'>1</option>
	<option value='2'>2</option>
</select><br />
<div id="more" style="display: none;" >
More info: <input type="text" name="comment" id="comment" />
</div>
<input type="submit" name="Submit" id="Submit" value="Submit" disabled="disabled">
</form>	
</body>
 
Why so complex??? You can run more than one function n the onchange event.


Try This:
HTML:
Field 2: <select name="f2" id="f2" onChange="enable_submit(this); show_comment(this); ">
	<option value="">Please select:</option>
	<option value='1'>1</option>
	<option value='2'>2</option>
</select><br />
<div id="more" style="display: none;" >
More info: <input type="text" name="comment" id="comment" />
</div>

Code:
function show_comment(eSelect) {
   var optComment = document.getElementById('more');
   if(eSelect.selectedIndex === 1) {
     optComment.style.display = 'block';
   } else {
     optComment.style.display = 'none';
   }
  }



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I had mentioned in my post that it doesn't work.
If I add the hidden field function to the onChange along with the show submit function, the show submit function works, but the show hidden field function only works if I select the desired option, select another option, then select the desired option again.
 
Oh, I see you changed the function. Let me try that way.
 
This works for me as expected.

Code:
<html>
<head>
<script type="text/javascript">
function enable_submit(obj) {
  var sb = document.getElementById("Submit");
  sb.disabled = false;
}

function show_comment(eSelect) {
   var optComment = document.getElementById('more');
   if(eSelect.selectedIndex === 1) {
     optComment.style.display = 'block';
   } else {
     optComment.style.display = 'none';
   }
  } 
</script>
</head>
<body>
<form action="form.php" method="post">
Field 1: <select name="f1" id="f1">
	<option value="">Please select:</option>
	<option value='1'>1</option>
	<option value='2'>2</option>
</select><br />
Field 2: <select name="f2" id="f2" onChange="enable_submit(this); show_comment(this); ">
	<option value="">Please select:</option>
	<option value='1'>1</option>
	<option value='2'>2</option>
</select><br />
<div id="more" style="display: none;" >
More info: <input type="text" name="comment" id="comment" />
</div>
<input type="submit" name="Submit" id="Submit" value="Submit" disabled="disabled">
</form>	
</body> 
</html>

If you have any other code that may be affecting it please show it. Otherwise there is no functional reason for the behavior you are describing.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
This works. One follow-up question. What do I need to change to match on the option value instead of the index. In the example they're the same, but in future applications they would be different.
 
You can get the value in one of 2 ways:

Code:
eSelect.options[eSelect.selectedIndex].value

or

eSelect.value;


The value property of the eSelect object holds the currently selected option's value.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top