Javascript Error, can't seem to find html tag |
||
Welcome to the Zymic webmaster forums. Our forums are here to provide people the free ability to discuss a range of websites related topics such as design, development coding and marketing.
In order to post you will need to register for a zymic account or if you already have one simply login by using the form on the left.
Zymic Webmaster Forums Web Design & Development Client Side Scripting JavaScript |
||
![]() |
Javascript Error, can't seem to find html tag |
||
Oct 6 2010, 11:47 PM
Post
#1
|
|
![]() Member ![]() ![]() Group: Members Posts: 99 Joined: 14-November 08 From: New Brunswick, Canada Member No.: 68,991 |
Am I missing something here?
CODE <!DOCTYPE html> <html> <script type="text/javascript" src="javacode2.js"> </script> <head> <title> yeah </title> </head> <body> <span id="test"></span> test </body> </html> javacode2.js CODE var span = document.getElementById('test'); span.innerHTML = 'why wont this crap work?'; Opera's handy developer's tool tells me this: Uncaught exception: TypeError: Cannot convert 'span' to object Error thrown at line 3, column 0 in file://localhost/G:/Documents/javacode2.js: span.innerHTML = 'why wont this crap work?'; it cant seem to find the span element. Does it matter that this is not uploaded to a site and is instead opened from my pc? |
|
|
May 14 2011, 10:04 PM
Post
#2
|
|
|
Member ![]() ![]() Group: Members Posts: 47 Joined: 15-March 11 Member No.: 182,002 |
I've been getting problems with innerHTML too, but I found another DOM access method.
Use this: CODE <html> <body> <span>Span 1</span> <span>2</span> <span>3</span> <span>5 ||||||</span> <span></span> <script type="text/javascript"> x=document.getElementsByTagName("span"); x[4].innerHTML="This <i>DOES</i> work"; </script> </body> </html> The result will be: QUOTE Span 1 2 3 5 |||||| This DOES work
|
|
|
May 24 2011, 08:39 AM
Post
#3
|
|
|
Newbie ![]() Group: Members Posts: 12 Joined: 20-February 11 From: North Carolina Member No.: 178,599 |
Probably doesn't work because the script is running before the span tag is found in the HTML. Or maybe span is a keyword that you shouldn't use like that in script. You could move the script tag below the span to the bottom of the page, but I would normally just wait until the page finishes loading to run most scripts (except for document.write) by changing the code like so:
CODE onload=function(){ var e = document.getElementById('test'); e.innerHTML = 'why wont this crap work?'; } document.getElementsByTagName is cool though. You don't even need an id="" with that. Am I missing something here? CODE <!DOCTYPE html> <html> <script type="text/javascript" src="javacode2.js"> </script> <head> <title> yeah </title> </head> <body> <span id="test"></span> test </body> </html> javacode2.js CODE var span = document.getElementById('test'); span.innerHTML = 'why wont this crap work?'; Opera's handy developer's tool tells me this: Uncaught exception: TypeError: Cannot convert 'span' to object Error thrown at line 3, column 0 in file://localhost/G:/Documents/javacode2.js: span.innerHTML = 'why wont this crap work?'; it cant seem to find the span element. Does it matter that this is not uploaded to a site and is instead opened from my pc? |
|
|
May 27 2011, 02:49 AM
Post
#4
|
|
|
Newbie ![]() Group: Members Posts: 3 Joined: 27-May 11 Member No.: 191,938 |
Probably doesn't work because the script is running before the span tag is found in the HTML. Or maybe span is a keyword that you shouldn't use like that in script. You could move the script tag below the span to the bottom of the page, but I would normally just wait until the page finishes loading to run most scripts (except for document.write) by changing the code like so: CODE onload=function(){ var e = document.getElementById('test'); e.innerHTML = 'why wont this crap work?'; } document.getElementsByTagName is cool though. You don't even need an id="" with that. script tag goes inside head tag CODE <html> <head> <script type="text/javascript"> //script that runs after onload goes here window.onload=function(){alert('test');} </script> </head> <body> <div id="elem1">Can find stuff</div> <script type="text/javascript"> //code run here can find stuff on webpage before this code like the elem1 but can't find elem2 </script> <div id="elem2">Not This Stuff</div> </body> </html> |
|
|
May 29 2011, 09:46 AM
Post
#5
|
|
![]() Newbie ![]() Group: Members Posts: 18 Joined: 25-July 10 Member No.: 150,506 |
The reason why it isn't working is because your browser is processing the JavaScript before the actual page content. To make sure that your JavaScript will work use window.onload = function(){ /*your stuff here*/ } or put all your JavaScript below your main page content or whatever you want to change or affect with your JavaScript.
|
|
|
Jun 13 2011, 03:37 PM
Post
#6
|
|
|
Member ![]() ![]() Group: Members Posts: 47 Joined: 15-March 11 Member No.: 182,002 |
Just use jQuery!!!!
Get jQuery from jquery.com Then, do CODE $(document).ready(function(){
$("#test").html("Why <u>will</u> this work!!!"); }); |
|
|
Aug 27 2011, 04:24 AM
Post
#7
|
|
|
Newbie ![]() Group: Members Posts: 19 Joined: 26-August 11 Member No.: 205,826 |
how to use an html file in another html file ?
|
|
|
Oct 12 2012, 12:44 PM
Post
#8
|
|
|
Newbie ![]() Group: Members Posts: 3 Joined: 12-July 11 Member No.: 198,324 |
It'll not work because <span> is an html tag.
As javascript is A CLASSLESS OBJECT ORIENTED language. "var span" is trying to convert it in an object, which is not allowed. trying giving some other name instead of exactly span. you should "span1" or "spn" instead of exact span. Boston Web Design This post has been edited by zpcs: Dec 18 2012, 01:43 PM
Reason for edit: Removal of SEO Link Building - Forbidden then allowance for Permitted Descriptive URL Link
|
|
|
![]() |
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users) |
||
| 0 Members: | ||
Forum Jump |
||
| Lo-Fi Version | Time is now: 20th May 2013 - 12:48 AM |