Click here to Skip to main content
13,043,865 members (48,129 online)
Rate this:
Please Sign up or sign in to vote.
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:

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 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2017
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