Connecting Tech Pros Worldwide Forums | Help | Site Map

How to chage images within a tags range?

Newbie
 
Join Date: Mar 2008
Posts: 3
#1: Mar 5 '08
Hi,

I'm currently trying to create a very particular rollover effect.
I need to programmatically modify a number of images.

A short example of the current html, which I'm trying to modify.

Expand|Select|Wrap|Line Numbers
  1. <a href="temp.html"onMouseOver="mouseover(this);"><span class="p1CE"><img src="1.png"></span><span class="p1CB"><img src="1.png"></span></a>
  2.  
In this example there are only 2 SPANs but there could be many more.

When the mouseover event is triggered, the mouseover function receives the link object. I can use this to go through the SPAN's using childNodes. However I cant figure out how to rewrite the SPANs content!

All I need to do is modify each
Expand|Select|Wrap|Line Numbers
  1. <img src="1.png">
to
Expand|Select|Wrap|Line Numbers
  1. <img src="1hl.png">
.

There is only ever 1 IMG inside a SPAN.

Any help would be greatly appreciated.
Thanks

PS. If I have missed the obvious its because this is my second day learning JavaScript so apologies in advance.

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Mar 5 '08

re: How to chage images within a tags range?


hi ...

let me give you an idea :)

Expand|Select|Wrap|Line Numbers
  1. // retrieve the src from the image node
  2. var s = img_node.src;
  3.  
  4. // now we match the chars before the .
  5. var m = s.match(/([^\.])/)[1];
  6.  
  7. // next we replace it like this 
  8. var src = s.replace(m, m + 'h1');
  9.  
  10. // now we set the src of the image node
  11. img_node.src = src;
  12.  
kind regards
Newbie
 
Join Date: Mar 2008
Posts: 3
#3: Mar 5 '08

re: How to chage images within a tags range?


Thanks gits for your post. To use your example I assume I need access to the image nodes. The problem I have is that I cannot figure out how to get to the image nodes using the DOM!

My current (non-working function!) is as follows.

Expand|Select|Wrap|Line Numbers
  1. function mouseover(obj)
  2. {
  3.     for(i=0; i<obj.childNodes.length; i++)
  4.     {
  5.        obj.childNodes[i].innerHTML = "<img src="1hl.png">";
  6.     }
  7. }
  8.  
Newbie
 
Join Date: Mar 2008
Posts: 3
#4: Mar 5 '08

re: How to chage images within a tags range?


Oops,
Just got it working, I was using the wrong quotes!
Sorry and thanks for the help.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#5: Mar 5 '08

re: How to chage images within a tags range?


hi ...

glad to hear you got it working :) ... post back to the forum anytime you have more questions ...

kind regards
Reply