reaching html elements

C

Chris

I am trying to get at all the 'a' tags surrounded by a div tag with an
id of 'id'

This doesn't seem to do it, picks up all 'a' tags

links=document.getElementsByTagName(id).getElementsByTagName("a");

Any thoughts?

Thanks,

Chris
 
M

Martin Honnen

Chris said:
I am trying to get at all the 'a' tags surrounded by a div tag with an
id of 'id'

This doesn't seem to do it, picks up all 'a' tags

links=document.getElementsByTagName(id).getElementsByTagName("a");

That should give an error as getElementsByTagName gives you a node list
and that node list does not have a getElementsByTagName method.
So you probably want
document.getElementById('id').getElementsByTagName('a')
If that does not work for you as intended then you need to provide more
details of the browser that does not work with.
 
C

Chris

That should give an error as getElementsByTagName gives you a node list
and that node list does not have a getElementsByTagName method.
So you probably want
document.getElementById('id').getElementsByTagName('a')
If that does not work for you as intended then you need to provide more
details of the browser that does not work with.

Thanks Martin,

I am working on a tooltip feature and my problem is that I can't
figure out howto only apply the necessary javascript to relevant links
and not every link on page. If I use

document.getElementById('id').getElementsByTagName('a')

this works for the first link I wrap in a tag with an id but not the
rest e.g.

<div id="tooltipthis"><a href="" title="">link</a></div>
<a href="" title="">No tooltip</a>
<div id="tooltipthis"><a href="" title="">link</a></div>

Thanks again,

Chris
 
M

Martin Honnen

Chris said:
<div id="tooltipthis"><a href="" title="">link</a></div>
<a href="" title="">No tooltip</a>
<div id="tooltipthis"><a href="" title="">link</a></div>

Ids need to be unique in the complete document so what you have above
can't work.
 
C

Chris

Ids need to be unique in the complete document so what you have above
can't work.

Martin,

Excuse my lack of javascript knowledge...I approach it like a bull in
a china shop.

After a break and some cheese and toast the obvious answer to my
problem was to do getElementByName("tooltip") and work from there.

Thanks again,

Chris
 
S

SAM

Chris a écrit :
I am trying to get at all the 'a' tags surrounded by a div tag with an
id of 'id'

This doesn't seem to do it, picks up all 'a' tags

links=document.getElementsByTagName(id).getElementsByTagName("a");

Any thoughts?

links=document.getElementById('id').getElementsByTagName('A');
 
M

Martin Honnen

Chris said:
After a break and some cheese and toast the obvious answer to my
problem was to do getElementByName("tooltip") and work from there.

There is no method named 'getElementByName', only one named
'getElementsByName' however div elements have no attribute named 'name'.
 
S

SAM

Chris a écrit :
document.getElementById('id').getElementsByTagName('a')

this works for the first link I wrap in a tag with an id but not the
rest e.g.

<div id="tooltipthis"><a href="" title="">link</a></div>
<a href="" title="">No tooltip</a>
<div id="tooltipthis"><a href="" title="">link</a></div>

this html is wrong : ID can only be unique


CSS :
=====

div { height: 500px; margin: 20px; border: 1px solid }


HTML :
======

<div id="tooltipthis_1">
<a href="#tooltipthis_3" title="">link 1</a>
</div>
<a href="#" title="">No tooltip</a>
<div id="tooltipthis_2">
<a href="#" title="">link 2</a>
</div>
<div id="tooltipthis_3">
<a href="#tooltipthis_1" title="">link 3</a>
</div>


JS :
====

function getLinks(commonId) {
var d = document.getElementsByTagName('DIV');
var L = [];
for(var i=, S=d.length; i<S; i++)
{
if(S.id &&
S.id.indexOf(commonId)>=0 &&
S.getElementsByTagName('A') &&
S.getElementsByTagName('A').length>0)
{
var A = S.getElementsByTagName('A');
for(var k=0, Z=A.length; k<A; k++)
{
L[L.length] = A[k];
}
}
}
return L;
}
window.onload = function() { var T = getLinks('tooltipthis'); };


HTML example of using :
=======================

<button onclick="location = T[0].href;">
click first link of tooltipthis
</button>
 
S

SAM

SAM a écrit :
function getLinks(commonId) {
var d = document.getElementsByTagName('DIV');
var L = [];
for(var i=, S=d.length; i<S; i++)


for(var i=0, S=d.length; i<S; i++)
 
G

Gregor Kofler

SAM meinte:
function getLinks(commonId) {
var d = document.getElementsByTagName('DIV');
var L = [];
for(var i=, S=d.length; i<S; i++)


for(var i=0, S=d.length; i<S; i++)

Why is S and L uppercase? Anyway, since the order doesn't matter

for(var i = d.length; i--;)

is shorter and perhaps faster, too.

Gregor
 
E

Evertjan.

Gregor Kofler wrote on 21 jun 2008 in comp.lang.javascript:
for(var i = d.length; i--;) { ... };

or:

var i = d.length; while (i--) { ... };

or:

var i = 0; do { ... } while (d[++i]);
 
S

SAM

Gregor Kofler a écrit :
SAM meinte:
function getLinks(commonId) {
var d = document.getElementsByTagName('DIV');
var L = [];
for(var i=, S=d.length; i<S; i++)


for(var i=0, S=d.length; i<S; i++)

Why is S and L uppercase?

Why not ?
(easier for my glasses ?)

or I give upper case to collections, or I do it for the length,
I prefer like that instead of all in lower case.

for(var i = d.length; i--;)

is shorter and perhaps faster, too.

shorter? to write ? surely.
faster ? for JS ? would have to be equal, no ?
 
S

SAM

Evertjan. a écrit :
Gregor Kofler wrote on 21 jun 2008 in comp.lang.javascript:
for(var i = d.length; i--;) { ... };

or:

var i = d.length; while (i--) { ... };

or:

var i = 0; do { ... } while (d[++i]);

Nothing more ? all was well seen about this ?

Each of you two doesn't have anything more to tell?

ie : a shorter way to get those @#?&! links.
 
G

Gregor Kofler

SAM meinte:
Why not ?

Uppercase variable names are supposed to indicate constructors.
or I give upper case to collections, or I do it for the length,
I prefer like that instead of all in lower case.

You can of course use whatever coding conventions you like. But since
you are *giving advices*, it would be smarter to follow best practices.
(Just changing cases for the sake of it, is a wacky concept anyway,
meseems...)

shorter? to write ? surely.
faster ? for JS ? would have to be equal, no ?

Faster. Yes. Since d.length has to evaluated only once.

Gregor
 
S

SAM

Gregor Kofler a écrit :
SAM meinte:


Faster. Yes. Since d.length has to evaluated only once.

with :
for(var i=0, L=d.length; i<L; i++)
L is evaluated several times ?
 
G

Gregor Kofler

SAM meinte:
with :
for(var i=0, L=d.length; i<L; i++)
L is evaluated several times ?

Sorry, my wrong - in this case it's evaluated only once (and therefore
just as fast as the alternative versions).

Gregor
 
T

Thomas 'PointedEars' Lahn

Chris said:
I am working on a tooltip feature and my problem is that I can't
figure out howto only apply the necessary javascript to relevant links
and not every link on page.

You don't need client-side scripting for tooltips in UAs newer than IE 6;
CSS will do there. (I am not talking about the `title' attribute which has
a different purpose.)


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top