SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    england
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replacing the src of an img after loading

    hi

    I'm sure this can be done.... just need a little help to figure out how!

    What I want, is the code to look for a particular ID of an img, and then change the filename (but not the whole path) of the image.

    For instance...
    if the path was "../images/headin.gif", the javascript would find this by the ID, and change to "headin" to "headdown".

    I know I can do something like...
    if(a.getAttribute("img").indexOf("headin")) {

    but then I need to know how to replace "headin" with "headdown".

    How can this be done?

    (the reason I don't want to replace the whole src, as that would be very simple, is that the path of the image will change depending on the directory the page is in)



    cheers for your help!

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <img id="pic" src="../images/headin.gif" />
    Code:
    var pic = document.getElementById('pic');
    pic.src = pic.src.replace(/(head)in\./, '$1down.');
    ::: certified wild guess :::

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    england
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that's exactly what I need!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •