Forum Moderators: open

Message Too Old, No Replies

dom probs

only because i is a bit fick i think...

         

natty

5:44 pm on Aug 20, 2004 (gmt 0)

10+ Year Member



hi all,

im having probs again, simply trying to get to the src of an img via the dom.

HTML is
<a href="javascript:alert(this.firstChild.src) ;"> <img src="images/image01.jpg" alt="." /></a>

all i want to do is get to the image src from the link via this.somthing..
firstChild.src isnt working?
stuck

any help much appreciated

nat.

Bernard Marx

9:44 pm on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It seems that inside an href, this refers to the window
(like it does when used 'inline', or when it's used in a function called globally).
2 solutions:

<a href="#" onclick="alert(this.firstChild.src);return false;"><img src="images/image01.jpg" alt="." /></a>
<img src="images/image01.jpg" alt="." onclick="alert(this.src)" style="cursor:pointer;cursor:hand;"/>

With #1, Mozilla can occasionally create phantom empty text nodes that could mess up the call to firstChild. Here for instance:

<a>  
<img>

Try keeping it more like this, to be on the safe side:

<a><img>

or even

<a>< 
img>