Anchor collection inside html table

R

ryan.mclean

Hi all, I hope somebody here can help me, I am attempting to obtain a
reference to the anchors inside of a table. Here is what I have:

table = document.getElementById(a_strTableId);

for(var i = 0; i < table.rows.length; i++) {
for(var j = 0; j < table.rows.cells.length; j++) {
a = table.rows.cells[j].getElementsByTagName("A")
alert(a.href);
if (strCurrentpath = a.href) {
a.className = 'yellow';
a.parentNode.className = 'space_on_bottom';
}
}
}

What I am expecting is to obtain the hrefs of the anchors inside of the
given table. Is there a better way to achieve this? Can I not
getElementsByTagName like I am attempting?

Any help is greatly appreciated.

Have a nice night,
Ryan
 
S

Stephen Chalmers

Hi all, I hope somebody here can help me, I am attempting to obtain a
reference to the anchors inside of a table. Here is what I have:

table = document.getElementById(a_strTableId);

for(var i = 0; i < table.rows.length; i++) {
for(var j = 0; j < table.rows.cells.length; j++) {
a = table.rows.cells[j].getElementsByTagName("A")
alert(a.href);
if (strCurrentpath = a.href) {
a.className = 'yellow';
a.parentNode.className = 'space_on_bottom';
}
}
}

What I am expecting is to obtain the hrefs of the anchors inside of the
given tabletable = document.getElementById('a_strTableId');

alert(a.href); // you need to specify a subscript here

if (strCurrentpath = a.href) {
a.className = 'yellow';


You have used assignment '=' instead of equalty '=='

This will work, but still relies on each cell containing only one link:

for(var i = 0; i < table.rows.length; i++)
for(var j = 0; j < table.rows.cells.length; j++)
{
a = table.rows.cells[j].getElementsByTagName("A")

alert(a[0].href);

if (strCurrentpath == a[0].href)
{
a.className = 'yellow';
a.parentNode.className = 'space_on_bottom';
}
}
 
R

RobG

Hi all, I hope somebody here can help me, I am attempting to obtain a
reference to the anchors inside of a table. Here is what I have:

table = document.getElementById(a_strTableId);

for(var i = 0; i < table.rows.length; i++) {
for(var j = 0; j < table.rows.cells.length; j++) {
a = table.rows.cells[j].getElementsByTagName("A")
alert(a.href);


a is a collection and does not have an href attribute.
if (strCurrentpath = a.href) {
a.className = 'yellow';
a.parentNode.className = 'space_on_bottom';
}
}
}

What I am expecting is to obtain the hrefs of the anchors inside of the
given table. Is there a better way to achieve this? Can I not
getElementsByTagName like I am attempting?


var table = document.getElementById(a_strTableId);
var a = table.getElementsByTagName('a');

'a' will be a collection of the A elements inside the table. Presumably
'strCurrentpath' is a string passed to the function and represents some
URI. Fixing the '=' issue and using a appropriately:

for (var i=0, j=a.length; i<j; i++ ){
if ( strCurrentpath == a.href ) {
a.className = 'yellow';
a.parentNode.className = 'space_on_bottom';
}
}

[...]
 
R

ryan.mclean

Oh my gosh, I made such silly mistakes . . . thank you both for showing
me the error of my ways. Just when you start to think you're a web
developer :)

Have a great day!
Ryan
 

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
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top