Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-26-2011, 12:01 AM   PM User | #1
Chudz
New to the CF scene

 
Join Date: Oct 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Chudz is an unknown quantity at this point
Question DOM GetElementsByTagName Problem

Hello,

I am just starting out learning JavaScript and I have just reach the DOM section of my course.

I have a page with 10 <a> tags on it and I have created the following JavaScript to tell me how many I have.

Code:
<script type="text/javascript">
	var myLinks = document.getElementsByTagName("a");
	console.log("We have ", myLinks.length ," many links on the page");
</script>
However in the console it reports this:

We have 0 many links on the page

This is not true as there are 10 links, 9 in the navgation section of the website and 1 in the footer.

If someone can tell me what I am doing wrong that would be great.

Thanks
Chudz is offline   Reply With Quote
Old 09-26-2011, 12:47 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
In your view, when does that script run?
Logic Ali is offline   Reply With Quote
Old 09-26-2011, 01:23 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What Logic Ali is pointing out:

If you have that code in the <head> of your page, then that's the right answer.

Only if you put that code *after* the last <a> on the page will it give you the answer you are looking for.

Or only if you delay its execution until the page is loaded.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-26-2011, 07:39 AM   PM User | #4
ironboy
Regular Coder

 
Join Date: Sep 2011
Location: Sweden
Posts: 154
Thanks: 1
Thanked 22 Times in 22 Posts
ironboy is an unknown quantity at this point
In other words, this would work:
Code:
onload = function(){
  var myLinks = document.getElementsByTagName("a");
  console.log("We have ", myLinks.length ," many links on the page")
};
ironboy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:40 AM.


Advertisement
Log in to turn off these ads.