1

I'm calling a function like this:

<img src="/images/icons/info.png"
width="18" height="18" class="iconbutton" alt="Add to Library" 
onclick="AddLibrary(12345,this.id); this.onclick=null;" />

The function then POSTS 12345 to another script and then is supposed to change the icon image:

function AddLibrary(pibnval,thisid) {
    $.post('/addlibrary.php', {
        pibn: pibnval
    }, function() {
    thisid.setAttribute('src', "/images/icons/tick.png");
    });
};

The POST works great, but the image does not change.

I also tried: document.getElementById(thisid).src = "/images/icons/tick.png"; but that didn't work either.

Any ideas?

4
  • where is the id of your img tag that you are passing to the function? Commented Feb 20, 2013 at 9:55
  • You should set an id to your image. Commented Feb 20, 2013 at 9:56
  • @jbl: No need, it's not used for anything. Commented Feb 20, 2013 at 9:57
  • @T.J.Crowder agree he really doesn't need one, but then he should rename his variables, and call on this, instead of this.id Commented Feb 20, 2013 at 10:05

3 Answers 3

5

thisid is just a string, not an element. Change your onclick to:

onclick="AddLibrary(12345,this); this.onclick=null;"

(I removed the .id) and your thisid variable name to img or some such (just so it's not misleading):

function AddLibrary(pibnval,img) {
    $.post('/addlibrary.php', {
        pibn: pibnval
    }, function() {
    img.setAttribute('src', "/images/icons/tick.png");
    });
};

Other notes:

  1. There's no reason to use setAttribute for this, image elements have a src property.

  2. You don't put ; after function declarations.

  3. Consistent indentation helps you, and others, read your code.

So:

function AddLibrary(pibnval,img) {
    $.post('/addlibrary.php', {
        pibn: pibnval
    }, function() {
        img.src = "/images/icons/tick.png";
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this - $('#'+ thisid).attr("src", '/images/icons/tick.png');

Also does your image have to be an image? because you could make it a div with a background image and set its width and height in css then just alter the css background like this

$('#'+ thisid).css({'background':'/images/icons/tick.png'});

Comments

0

I'd rather attach the handler with JS (instead of inline) but you could change the code to this:

HTML

<img src="/images/icons/info.png"
width="18" height="18" class="iconbutton" alt="Add to Library" 
onclick="AddLibrary(event, 12345);" />

JS

function AddLibrary(e, pibnval) {
    var _this = e.target;

    $.post('/addlibrary.php', {
        pibn: pibnval
    }, function() {
        _this.setAttribute('src', "/images/icons/tick.png");
        _this.onclick = null;
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.