Ok so i'm reading a book that's teaching me JavaScript & AJAX...
and for all except 2 of the examples (or is one since the 2 scripts are needed together?) and basically it demonstrating how to use multi-level conditionals
so here's the Html for the script
i looked over it word for word line by line and found no error...so i looked at the JavaScriptCode:<!DOCTYPE htm_l PUBLIC "-/ / W_3C/ /DTD XHTML 1.0 Transitional/ / EN" "h_t_tp://ww_w.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="ht_tp://w_ww.w3.org/1999/xhtml"> <head> <title>Switch/Case Handling</title> <script type="text/javascript" src="\where i have my stuff\JavaScript\quotes.js"></script> </head> <body bgcolor="white"> <h2>Famous Presidential Quotes</h2> <form action="#"> <input type="button" id="Lincoln" value="Lincoln" /> <input type="button" id="Kennedy" value="Kennedy" /> <input type="button" id="Nixon" value="Nixon" /> </form> </body> </html>
... ohh and i added some underscores so then the whole thing could be put up there... pesky links
and found nothing amiss... well except for some typo's but i fixed those real quick... so i was wondering... does anyone know what the H#!! happened?Code:window.onload = initAll; function initAll() { document.getELementById("Lincoln").onclick = saySomething; document.getElementById("Kennedy").onclick = saySomething; document.getElementById("Nixon").onclick = saySomething; } function saySomething() { switch(this.id) { case "Lincoln": alert("Four score and seven years ago..."); break; case "Kennedy": alert("Ask not what your country can do for you..."); break; case"Nixon": alert("I am not a crook!"); break; default; } }
getELementById("Lincoln")
should be
getElementById("Lincoln")
ok i fixed that, however, i still get nothing since the scripts are designed to have a pop up message come up everytime i click one of the buttons... and i tried this on Opera,Firefox and IE8 and all them seem to get to the HTML but that's it nothing happens when i click them![]()
You need an onClick event to call the JavaScript.
I think it would be easier to directly include the event handlers in the HTML elements. That way you don't have to worry about what object this refers to.
Life's too short to be cool. Be a nerd.
well thanks guys but for some reason none of my browsers will show thge codes i typed in + i found out that those 2 codes were "incomplete" and although i dopn't see what was different from my codes and the ones in the book... well i decided to download the script for the book from the site and just stared very hard at it decided my browsers were possessed since those codes worked perfectly well on my browser....
should beCode:default;
the only other problem is what WingedPanther mentioned about getElementByIdCode:default:
Should:
Not be:Code:document.getELementById("Lincoln").onclick = saySomething;
Not certain if the use of quotes/parentheses are a requirement or just a formality in javascript, but this is the way I've always done it.Code:document.getELementById("Lincoln").onclick = "saySomething()";
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks