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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getElementById returning no length for existing id

    Is there something wrong with my script? Typo?

    Code:
    function accite() {
    	bq = document.getElementById("content").getElementsByTagName("blockquote");
    	q = document.getElementById("content").getElementsByTagName("q");
    	alert(
    		"<blockquote /> = " + bq.length +
    		"<q /> = " + q.length
    	);
    }
    It's saying "document.getElementById('content') has no properties", and yes, I definitely have a <div id="content" />.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Show us more of your source. Specifically, show us the HTML in question.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is an example page: http://sethrasmussen.com/index.php?p=86

    I should note that I am having the problem in Firefox and IE. It actually seems to work fine in Opera, though. Dunno about more yet.
    Last edited by ]|V|[agnus; 11-26-2004 at 11:48 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    If you only have one instance of the blockquote or q elements, getElementsByTagName will return that object instead of an array of objects.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>48059</title>
    	</head>
    	<body>
    		<div id="content">
    			<blockquote></blockquote>
    			<q></q>
    		</div>
    
    		<script type="text/javascript">
    			var bq = document.getElementById("content").getElementsByTagName("blockquote");
    			if(bq.length == undefined)
    			{
    				bq = new Array(bq);
    			}
    
    			var q = document.getElementById("content").getElementsByTagName("q");
    			if(q.length == undefined)
    			{
    				q = new Array(q);
    			}
    
    			alert(
    				"<blockquote /> = " + bq.length +
    				"<q /> = " + q.length
    			);
    		</script>
    	</body>
    </html>

  • #5
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, okay, but the problem is that IE and Firefox are choking before they even get to that. They bomb on "getElementById('content')"

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For one, your calling the function window.onload is premature and the content division has not yet been written to the page... Sans the error... Try body.onload instead...

    Code:
    function accite() {
    	bq = document.getElementById("content").getElementsByTagName("blockquote");
    	q = document.getElementById("content").getElementsByTagName("q");
    	alert(
    		"<blockquote /> = " + bq.length +
    		"<q /> = " + q.length
    	);
    }
    
    // init
    function init () {accite();}
    window.onload=init();
    .....Willy

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "body is not defined"

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can't change window.onload = init() with body.onload = init() in the HEAD because well.... the body has not been written to the page yet...

    Try: <body onload="init()">

  • #9
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    very well, but how can i avoid adding the event handler to the structure?

    document.getElementsByTagName("body").onload=init(); doesn't seem to work.

    perhaps, document.getElementById("sethrasmussen-dot-com").onload=init(); ?

    assuming of course that's the name of the body element?

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no no that doesn't work... perhaps i include a file at the end of every page with sort of _exit scripts?

  • #11
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ]|V|[agnus
    very well, but how can i avoid adding the event handler to the structure?

    document.getElementsByTagName("body").onload=init(); doesn't seem to work.

    perhaps, document.getElementById("sethrasmussen-dot-com").onload=init(); ?

    assuming of course that's the name of the body element?
    getElementsByTagName returns an array....
    Try: document.getElementsByTagName("body")[0].onload=function(){init()};

  • #12
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well i'm just doing the onload attribute for now. what i'm trying to do with my script is to insert links after blockquotes and quotes that have their href's generated by the blockquote or quote's cite attributes. i'm getting no errors with the following, but no links are inserted:

    Code:
    function accite() {
    	d = document;
    	bq = d.getElementById("content").getElementsByTagName("blockquote");
    	q = d.getElementById("content").getElementsByTagName("q");
    	// cite blockquotes
    	for(i=0;i<bq.length;i++){
    		newP=d.createElement("p");
    		newA=d.createElement("a");
    		newA.setAttribute("href",bq[i].getAttribute("cite"));
    		newP.appendChild(newA);
    		bq[i].appendChild(newP);
    	}
    }
    that code is being run on the same link from above.

  • #13
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Although you are now calling the accite function body.onload.... You are still using window.onload to call the init function which in turn calls the accite and thus is still throwing the aforementioned error...

    This could be causing a conflict but is hard for me to tell since your entire page does not work for me using IE6... There is much text overlayed upon other text, I can not scroll, view source does not open notepad although the context menu is there and the page seems to take a lot of cpu to display....

    Anyways, it is hard for me to see what is going on or what it is supposed to look like...

    .....Willy

  • #14
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Willy Duitt
    Although you are now calling the accite function body.onload.... You are still using window.onload to call the init function which in turn calls the accite and thus is still throwing the aforementioned error...
    No, I'm not.

    Quote Originally Posted by Willy Duitt
    This could be causing a conflict
    What conflict? I am not getting any errors anymore, but the desired effect is not happening. I think it's because I am trying to use appendChild on a new element that has not been added to the tree yet itself. Yes?

    Quote Originally Posted by Willy Duitt
    but is hard for me to tell since your entire page does not work for me using IE6... There is much text overlayed upon other text, I can not scroll, view source does not open notepad although the context menu is there and the page seems to take a lot of cpu to display....

    Anyways, it is hard for me to see what is going on or what it is supposed to look like...
    ...



    Quote Originally Posted by sethrasmussen.com/in/colophon.php
    That said, users of Internet Explorer can expect persistent bugginess, as this site makes use of modern techniques, the bulk of which IE only partially supports. Despite its massive market share, I've decided not to go out of my way to accomodate this archaic browser since the rest of the competition has continued to innovate and move forward. This site is about progress, not market share.

  • #15
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you are no longer calling the init function window.onload you changed it since I last looked at it shortly after you posted "that code is being run on the same link from above".... the window.onload = init() is in your external javascript file...

    And FWIW: What you call modern browsers I call equally buggy if not moreso than IE... Heck, if Firefox is so wonderful why did this latest release break nearly every plugin that was made in the past? Now, all those plugins need to be patched... How long can we expect that to continue?

    Besides, IE is one of the most forgiving browsers there is... If your code completely breaks using IE, there is something wrong and the quick look I took at your code shows no indication of cutting-edge code writing....

    That being said, I was only trying to help you by pointing out some obvious mistakes... If you have removed the window.onload from the external file, great for you but roll your eyes at someone else....

    .....Willy


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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