Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

AJAX image replace

  • 04-12-2008 1:53pm
    #1
    Registered Users, Registered Users 2 Posts: 9,225 ✭✭✭


    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,225 ✭✭✭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,859 ✭✭✭Duckjob


    Try adding a
    document.recalc(true);

    after you update the image


  • Registered Users, Registered Users 2 Posts: 9,225 ✭✭✭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,859 ✭✭✭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