...

View Full Version : One of two event handlers isn't firing



ruffy
09-03-2008, 10:05 PM
Here's the HTML/PHP file, and below this is the javascript file.
My trigger2 isn't firing.


<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:


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!

ruffy
09-04-2008, 05:19 PM
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".

Kor
09-05-2008, 11:06 AM
You may simply check the existence of the element:


function setTrigger2() {
if(!document.getElementById("org")){return}
var org = document.getElementById("org");
org.onchange = function() {
shout("College Name CHANGED");
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum