/    Sign up×
Community /Pin to ProfileBookmark

getElementById and its links

Hello,
I have the following:

[CODE]
<div id=”myId”>
<SOME LINKS>
<SOME IMAGES>
</div>
[/CODE]

As I expect like :
document.links.length will give out the number of links on the whole document

I expect too :
p = document.getElementById(‘myId’);
p.links.length to give out the number of links inside the div tag with myId. The actual result, it’s null or not an object.

Why? and How can I access the subelements of an element I already got it by its Id?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisSep 30.2006 — It should be:p.document.links.length
Copy linkTweet thisAlerts:
@Mr_JSep 30.2006 — Try it this way

[code=php]myDiv=document.getElementById("myId")
myLinks=myDiv.getElementsByTagName("A")

numOfLinks=myLinks.length

alert(numOfLinks)
[/code]
Copy linkTweet thisAlerts:
@samanyoluSep 30.2006 — [code=php]

<body>

<div id="myId">
<a href="http://www.webdeveloper.com/">webdeveloper</a><br>
<a href="http://www.webdeveloper.com/forum">forum</a><br>
<a href="http://www.google.com/">google</a><br>
</div>

<script type="text/javascript">

var myDiv=document.getElementById("myId")
var myLinks=myDiv.getElementsByTagName("A")
var numOfLinks=myLinks.length
alert(numOfLinks) // 3

</script>
[/code]
Copy linkTweet thisAlerts:
@said_foxauthorOct 01.2006 — Thank you all,

I'm, indeed, need to access or control each link, not just only count their population, i.e using innerHTML or setAttribute method, or even deleting it.
Copy linkTweet thisAlerts:
@Mr_JOct 01.2006 — Can you be a bit more specific about what you are wanting to achieve?

In the meantime here's a further example

[code=php]<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">

onload = function initTest(){
myDiv=document.getElementById("myId")
myLinks=myDiv.getElementsByTagName("A")
numOfLinks=myLinks.length

displayResult=""

for(var i=0;i<numOfLinks;i++){
displayResult+="Link number "+(i+1)+"<br>"
+myLinks[i].href.toUpperCase()+"<br>"
+myLinks[i].innerHTML.toUpperCase()+"<br><br>"
}

document.getElementById("display").innerHTML=displayResult
}


</script>

</HEAD>
<BODY>
<div id="myId">
<a href="http://www.webdeveloper.com/">webdeveloper</a><br>
<a href="http://www.webdeveloper.com/forum">forum</a><br>
<a href="http://www.google.com/">google</a><br>
</div>
<BR><BR>
The following results will display the links href value and the links text value

<BR><BR>
<div id="display"></div>

</BODY>
</HTML>[/code]
×

Success!

Help @said_fox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...