473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

reaching html elements

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

Expand|Select|Wrap|Line Numbers
  1. links=document.getElementsByTagName(id).getElementsByTagName("a");
Any thoughts?

Thanks,

Chris
Jun 27 '08 #1
15 981
Chris wrote:
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

Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. 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.



--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
On 21 Jun, 11:49, Martin Honnen <mahotr...@yahoo.dewrote:
Chris wrote:
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
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. 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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
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.

Expand|Select|Wrap|Line Numbers
  1. <div id="tooltipthis"><a href="" title="">link</a></div>
  2. <a href="" title="">No tooltip</a>
  3. <div id="tooltipthis"><a href="" title="">link</a></div>
Thanks again,

Chris
Jun 27 '08 #3
Chris wrote:
<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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #4
On 21 Jun, 12:37, Martin Honnen <mahotr...@yahoo.dewrote:
Chris wrote:
<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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
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
Jun 27 '08 #5
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).getElement sByTagName("a");

Any thoughts?
links=document.getElementById('id').getElementsByT agName('A');

--
sm
Jun 27 '08 #6
Chris wrote:
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'.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #7
SAM
Chris a écrit :
>
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. <div id="tooltipthis"><a href="" title="">link</a></div>
  2. <a href="" title="">No tooltip</a>
  3. <div id="tooltipthis"><a href="" title="">link</a></div>
this html is wrong : ID can only be unique


CSS :
=====

Expand|Select|Wrap|Line Numbers
  1. div { height: 500px; margin: 20px; border: 1px solid }

HTML :
======

Expand|Select|Wrap|Line Numbers
  1. <div id="tooltipthis_1">
  2. <a href="#tooltipthis_3" title="">link 1</a>
  3. </div>
  4. <a href="#" title="">No tooltip</a>
  5. <div id="tooltipthis_2">
  6. <a href="#" title="">link 2</a>
  7. </div>
  8. <div id="tooltipthis_3">
  9. <a href="#tooltipthis_1" title="">link 3</a>
  10. </div>
  11.  
JS :
====

Expand|Select|Wrap|Line Numbers
  1. function getLinks(commonId) {
  2. var d = document.getElementsByTagName('DIV');
  3. var L = [];
  4. for(var i=, S=d.length; i<S; i++)
  5. {
  6. if(S[i].id &&
  7. S[i].id.indexOf(commonId)>=0 &&
  8. S[i].getElementsByTagName('A') &&
  9. S[i].getElementsByTagName('A').length>0)
  10. {
  11. var A = S[i].getElementsByTagName('A');
  12. for(var k=0, Z=A.length; k<A; k++)
  13. {
  14. L[L.length] = A[k];
  15. }
  16. }
  17. }
  18. return L;
  19. }
  20. window.onload = function() { var T = getLinks('tooltipthis'); };

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

Expand|Select|Wrap|Line Numbers
  1. <button onclick="location = T[0].href;">
  2. click first link of tooltipthis
  3. </button>
--
sm
Jun 27 '08 #8
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++)
--
sm
Jun 27 '08 #9
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]);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #10
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 ?

--
sm
Jun 27 '08 #11
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.

--
sm

Jun 27 '08 #12
SAM meinte:
>Why is S and L uppercase?

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...)

>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 ?
Faster. Yes. Since d.length has to evaluated only once.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #13
SAM
Gregor Kofler a écrit :
SAM meinte:
>>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 ?

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 ?

--
sm
Jun 27 '08 #14
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

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #15
Chris wrote:
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements:...
59
by: phil-news-nospam | last post by:
In followups by Brian O'Connor (ironcorona) to other posts, he repeats the idea that using tables in CSS is not something that should be done because IE doesn't support it. Of course I'm not happy...
6
by: Sonnich | last post by:
Hi! I just want a single button on a page (without a form) to be disabled using JS. My problem is to reach the components/elements on the page. Like this: <HTML> <HEAD> <SCRIPT...
3
by: Steve | last post by:
Hi; I'm working on a demo of using a timer on a web site that is made visible by making a div visible. My "PopIn Box" div is empty on the page. Before making it visible I used javascript to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.