Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

AJAX image replace

  • 04-12-2008 02:53PM
    #1
    Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭


    trying to replace an image using onclick and js, i am currently using this:
    document.images[id].src = 'images/voted.png';
    

    inside a function called by this:
    <a href='#n' onclick='addKarma(1,0,28,TIP3)'><img src='images/notvoted.png' id='TIP3'></a>
    
    while the rest of the function is carrying out its task, it wont update the image, anyone know where i am going wrong?


Comments

  • Closed Accounts Posts: 184 ✭✭vodkadub


    Maybe try something like this;

    <script language="javascript">

    function changeImg(){
    document.getElementById("image").src="replaceimage.jpg";
    }

    </script>

    <a href="" onClick="changeImg()"><img src="oldimage.jpg" id="image"></a>


  • Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭Chardee MacDennis


    vodkadub wrote: »
    Maybe try something like this;

    <script language="javascript">

    function changeImg(){
    document.getElementById("image").src="replaceimage.jpg";
    }

    </script>

    <a href="" onClick="changeImg()"><img src="oldimage.jpg" id="image"></a>

    well it has to be part of the larger function, i changed it from
    document.images[id].src = 'images/voted.png';
    

    to
    document.getElementById(id).src='images/voted.png';
    

    but no luck...


  • Registered Users, Registered Users 2 Posts: 2,857 ✭✭✭Duckjob


    Try adding a
    document.recalc(true);

    after you update the image


  • Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭Chardee MacDennis


    Duckjob wrote: »
    Try adding a



    after you update the image

    nope no good, does it make a difference if the js is in an external file?


  • Registered Users, Registered Users 2 Posts: 2,857 ✭✭✭Duckjob


    This code works so you might be able to compare it with yours:
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">

    function changeimage(sImageName) {
    document.getElementById(sImageName).src = "images/2.png";
    }
    </script>
    </head>
    <body>


    <a href="#" onclick="changeimage('img1');"><img id="img1" src="images/1.png" alt="" /></a>

    </body>
    </html>


  • Advertisement
Advertisement