How to chage images within a tags range?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foldaway
    New Member
    • Mar 2008
    • 3

    How to chage images within a tags range?

    Hi,

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

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

    Code:
    <a href="temp.html"onMouseOver="mouseover(this);"><span class="p1CE"><img src="1.png"></span><span class="p1CB"><img src="1.png"></span></a>
    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
    Code:
    <img src="1.png">
    to
    Code:
    <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
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    let me give you an idea :)

    [CODE=javascript]
    // retrieve the src from the image node
    var s = img_node.src;

    // now we match the chars before the .
    var m = s.match(/([^\.])/)[1];

    // next we replace it like this
    var src = s.replace(m, m + 'h1');

    // now we set the src of the image node
    img_node.src = src;
    [/CODE]
    kind regards

    Comment

    • foldaway
      New Member
      • Mar 2008
      • 3

      #3
      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.

      [CODE=javascript]
      function mouseover(obj)
      {
      for(i=0; i<obj.childNode s.length; i++)
      {
      obj.childNodes[i].innerHTML = "<img src="1hl.png">" ;
      }
      }
      [/CODE]

      Comment

      • foldaway
        New Member
        • Mar 2008
        • 3

        #4
        Oops,
        Just got it working, I was using the wrong quotes!
        Sorry and thanks for the help.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          hi ...

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

          kind regards

          Comment

          Working...