Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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:
 
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";	  
}
}
	
 
<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 15-Apr-13 4:13am
jba1991389
Comments
Prasad Khandekar at 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 at 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)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100