www.webdeveloper.com
Results 1 to 4 of 4

Thread: getElementsById and modifying strings...

  1. #1
    Join Date
    Apr 2010
    Posts
    4

    getElementsById and modifying strings...

    Hi guys,

    I'm new to Jscript but trying to combien it with CSS to build a website. I need to do the following:

    1) Read the img src property in the mainPicture DIV
    2) Replace a string in the actual name of the image
    3) Store the new result and pathname

    The code I have so far is this:

    function getPic(){
    var currentPicture = document.getElementById("mainPicture").getElementsByTagName("img")[0].src;
    alert(currentPicture);
    }

    This works well for part 1, but gives me the path of the image *relative* to the Document vs. the Site Root (C:/website/me/images/pic1_before.jpg instead of /images/pic1_before.jpg). I need to get the Site Root path.

    For part 2, what code do I write to take the string pic1_before.jpg and change it to pic1_after.jpg?

    For part 3, how do I then save the whole string /images/pic1_after.jpg? I will be applying it somewhere else.

    Hope this makes sense and thanks in advance for helping.


    milton

  2. #2
    Join Date
    Sep 2008
    Posts
    408
    Close.

    Code:
    function getPic() {
         document.getElementById("mainPicture").src;
         window.alert(currentPicture);
    }
    If the img tag has id="mainPicture"

    To change:
    Code:
    function changePic() {
         document.getElementById("mainPicture").src = '/images/pic1_after.jpg';
    }

  3. #3
    Join Date
    Apr 2010
    Posts
    4
    Hi there,

    Code:
    function getPic() {
         document.getElementById("mainPicture").src;
         window.alert(currentPicture);
    }
    If the img tag has id="mainPicture"

    That doesn't work for me. When I do a mouseDown on my image that calls the getPic function, the alert box gives me the info I need with my code, but not with the one above.

    To change:
    Code:
    function changePic() {
         document.getElementById("mainPicture").src = '/images/pic1_after.jpg';
    }
    [/QUOTE]

    I need to run some code to actually find/change the name of the jpg, dynamically, not statically like above, because the mainPicture DIV may be holding an image called pic1_after.jpg or pic3_after.jpg...anywhere up to 60! That's why I need to do this dynamically, hence my post.

    Any more help you could provide though would be helpful.

    cheers,


    milton

  4. #4
    Join Date
    Apr 2010
    Posts
    4
    Hi guys,

    I finally managed to figure this out. Here is the code I used:

    function swapBefore(){
    var currentPic, changePic;
    currentPic = document.getElementById("mainPicture").getElementsByTagName("img")[0].src;
    changePic = currentPic.replace ("after", "before");
    document.images.mainPicture.src = changePic;
    }

    Thanks for all your help guys,

    cheers,


    milton

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles