Click to See Complete Forum and Search --> : innerHTML Issue with IMG tag in A Tag


tpra21
07-12-2008, 08:59 AM
I have the below code that is loaded at the beginning of a PHP page. The first loop works great, but the second loop does not work. I am assuming the innerHTML function grabs what is between the <a></a> tags, which in the case of the second loop, is the <img> tag.

Any help would be geatly appreciated. Thanks, Adam

function loadLinks(){
var a = document.getElementsByTagName('a'), rel;
for(var i = 0, len = a.length; i < len; ++i){
content = a[i].innerHTML + "";
shadowbox_config = "shadowbox;height=450;width=700";
if (content == "Contact Information"){
a[i].setAttribute('rel',shadowbox_config);
a[i].setAttribute('title','Contact Info');
}
}
var a = document.getElementsByTagName('a'), rel;
for(var i = 0, len = a.length; i < len; ++i){
content = a[i].innerHTML + "";
shadowbox_config = "shadowbox;height=450;width=700";
if (content == "<img src ='images/map.jpg' border = '0' align = 'middle'></img>"){
a[i].setAttribute('rel',shadowbox_config);
a[i].setAttribute('title','Map Info');
}
}

}

Declan1991
07-12-2008, 09:19 AM
What's the HTML?

Fang
07-12-2008, 09:28 AM
content == "<img src ='images/map.jpg' border = '0' align = 'middle'></img>"Comparison to invalid markup and anyway each browser alters the generated code in different ways.
Use a more specific comparison.

tpra21
07-12-2008, 09:29 AM
Below is the HTML for the link in question (in a PHP statement):

return ("<a href=\"" . $linkString . "\" target=\"_blank\"><img src ='images/map.jpg' border = '0' align = 'middle'></img></a>&nbsp; Property Map");

If I replace the above with:

return ("<a href=\"" . $linkString . "\" target=\"_blank\">view map</a>&nbsp; Property Map");

and my javascript with content = "view map", the second loop works. The second loop is somehow not recognizing the image tag as the innerHTML for the <a> tag.

Thanks,

Fang
07-12-2008, 09:35 AM
If you write:<img src ='images/map.jpg' border = '0' align = 'middle'></img>Fx generates: <img src="images/map.jpg" align="middle" border="0">
IE generates:<IMG src="images/map.jpg" align=middle border=0></IMG>