SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Oct 10, 2007, 21:11 #1
- Join Date
- Oct 2007
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code example from book - getElementsById
Hey. I'm reading "Simply Javascript", and I've been trying to code an example using the getElementsById method.
First, the HTML in the body:
Code:<p id="uniqueElement">Here's a link to the <a id="dogs" href="http://www.hsus.org/">Humane Society of the United States</a> for you.</p>
Code:#uniqueElement{ font-size: 24px; }
Code:var target = document.getElementById("uniqueElement"); alert(target.nodeName);
NOTE: I tried deleting the code and simply placing in
Code:alert("Why are there problems!?!?!");
-
Oct 10, 2007, 22:11 #2
If you're using FireFox, you can open error console and see what's going on.
And be sure you're getting the element with getElementById, not getElementsById.H u m o
Uncensored Forums for Intelligent People
-
Oct 10, 2007, 23:59 #3
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- Mentioned
- 25 Post(s)
- Tagged
- 1 Thread(s)
Where is the code called from? If you try to access the element before it loads then it can't get the information.
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
-
Oct 11, 2007, 02:46 #4
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
That's probably it. I suspect this code is above the </head> tag but is not contained within a window.onload function block.
-
Oct 11, 2007, 11:24 #5
- Join Date
- Oct 2007
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok. I suspected something like that, as they warn about manipulating HTML that has not yet loaded. Now, they mention that there is a script in the "Core" library. How can I run that script first? Do I need to place the entire core before the script I want to run?
Also, how can I use onLoad? Is that easier?
Thanks for the help, guys.
-
Oct 11, 2007, 11:37 #6
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:<head> ... <script type="text/javascript"> window.onload = function () { // put all your javascript that is to run when the window has completed loading HERE alert("window has loaded"); } </script> ... </head>
Bookmarks