Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    39
    Thanks
    4
    Thanked 0 Times in 0 Posts

    One of two event handlers isn't firing

    Here's the HTML/PHP file, and below this is the javascript file.
    My trigger2 isn't firing.

    Code:
    <html>
    <head>
    <script src='js/test.js' type='text/javascript'></script>
    </head><body>
    <?php
    include_once "configHPP.php";				
    echo '<h2>How Much Education Have You Acquired, GENIUS?</h2>';
    echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
    if (!isset($_POST['go']))  {
    	echo "<select id='education' name='education'>";
    	echo "<option value='Jr.High'>Jr.High</option>";
    	echo "<option value='HighSchool'>HighSchool</option>";
    	echo "<option value='College'>College</option></select>";
    }  else  {
    	if (isset($_POST['education'])) {
    		if ($_POST['education'] == "College") {
    			echo "<label>College name: </label><input id='org' type='text' size='30' /><br />";
    			echo "<label>Zip code: </label><input type='text' size='10' /><br />";
    		} else {
    			exit("Not bright - are you?");
    		}
    	}
    }
     ?>
    <input id="submitbutton" name="go" type="submit" value=" Submit " />
    </form></body></html>
    
    javascript file:

    Code:
    function shout(msg)  { 
    	alert(msg);
    }
    
    function setTrigger1()  {
    	var picklist = document.getElementById("education");
    	picklist.onchange = function() {
    		if (picklist.value != "")  {
    			shout("OPTION changed"); 
    		}
    	}
    }
    
    function setTrigger2()  {
        var org = document.getElementById("org");
     	org.onchange = function() {
     		shout("College Name CHANGED");
    	}  
    }
    
    window.onload = function() {
    	setTrigger1();
    	setTrigger2();
    }
    Thanks for your help!

  • #2
    New Coder
    Join Date
    Jul 2008
    Posts
    39
    Thanks
    4
    Thanked 0 Times in 0 Posts
    It turns out the reason it failed had to do with
    my code failing to first check whether
    the DOM object exists at all.
    I notice Safari during development
    remains silent on some errors, whereas Firefox
    comes to my rescue for NOT giving me the
    "silent treatment".

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may simply check the existence of the element:
    Code:
    function setTrigger2()  {
    if(!document.getElementById("org")){return}
        var org = document.getElementById("org");
     	org.onchange = function() {
     		shout("College Name CHANGED");
    	}  
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •