Search code examples
imagesrcattr

change image on id after succes:


I have this piece of code, but can't seem to get it working.

success: function(){
$('.like').find('.like'+like_id).attr("src", "/img/icons/checked.gif"); ...etc.

.like is the class for all the images. OnClick I would like to have the img + id changed. It keeps changing all the images with class .like.

Even when using this, it is passing the right ID, but still changing all the .like images instead of the one with the right id: var value = $(this).attr ( "id" );

Any help is highly appreciated!


Solution

  • Your problem is that with find('.like'+like_id) you are finding every item that has the class like10 (for like_id 10). what you want is $('#'+like_id).attr();. What you want to consider is that an id must be unique within the whole html element, so using only the unqiue_id as id is not the best way. A better way would be <img src="" class="like" id="like1"> and then using $('#like'+like_id).attr();

    So, you're code could look like this:

    success: function(){
      $('#like' + like_id).attr("src", "/img/icons/checked.gif");
      ...
    }
    
    <img src"" class="like" id="like{unique nr}">