Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there!

I have the same code in two different files; one file is a test page to check that the function works (which is does) and the other is a php page where the same function doesn't work.

The function I have hides and shows different div's depending on what is selected in the select list. This is the code:

JavaScript
function functionName(){
var foo = document.getElementById('div1');
var bar = document.getElementById('div2');
if (document.getElementById('selectID').value == "val1") {
foo.style.visibility = "hidden";
bar.style.visibility = "visible";
}
  else{
foo.style.visibility = "visible";
bar.style.visibility = "hidden";	  
}
}


HTML
<select name="selectName" id="selectID" onchange="functionName()">
<option value="">Empty value</option>
<option value="val1">value 1</option>
<option value="val2">value 2</option>
</select>

<div id="div1" style="visibility:visible">
<input type="text" name="" id="" />
<input type="text" name="" id="" />
</div>

<div id="div2" style="visibility:hidden">
<input type="text" name="" id="" />
</div>


The code is exactly the same in a .js file that is attached to a .php page. Other functions from the same .js file work.
Posted
Comments
Prasad Khandekar 15-Apr-13 10:42am    
Do you see any errors in browser? Which browser are you using? Try changing if condition as follows

var ctrl = document.getElementById('selectID');

if (ctrl.options[ctrl.selectedIndex].value === "val1")

Regards,
jba1991 15-Apr-13 11:00am    
Thanks.

I think I found the problem, but im not sure that is what caused the error.

I had two different functions doing the same thing, but one had an 'else if' in it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900