Click here to Skip to main content
11,795,568 members (78,216 online)
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 3: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 Maciej Los 340
1 Sergey Alexandrovich Kryukov 316
2 OriginalGriff 285
3 Abhinav S 223
4 KrunalRohit 214
0 OriginalGriff 1,797
1 Maciej Los 1,415
2 KrunalRohit 1,240
3 CPallini 840
4 ppolymorphe 808

Advertise | Privacy | Mobile
Web03 | 2.8.151002.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