Click here to Skip to main content
Rate this: bad
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") { = "hidden"; = "visible";
  else{ = "visible"; = "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>
<div id="div1" style="visibility:visible">
<input type="text" name="" id="" />
<input type="text" name="" id="" />
<div id="div2" style="visibility:hidden">
<input type="text" name="" id="" />
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
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")
jba1991 at 15-Apr-13 11:00am
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 476
1 Sergey Alexandrovich Kryukov 235
2 CHill60 195
3 Afzaal Ahmad Zeeshan 145
4 TheRealSteveJudge 120
0 Sergey Alexandrovich Kryukov 9,121
1 OriginalGriff 7,920
2 Peter Leow 4,774
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,176

Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2015
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