1

I am using jquery and what I want to do here is to be able to set the image that will show up based upon the link that is being hovered to.

<script type='text/javascript'>
$(function(){
  $('img').hide();
  $('a').mouseenter(function(){
    var currentimg= $(this).html();
    alert(currentimg);
    $("img[src=currentimg.jpg]").show(); //I want to use currentimg variable here for the name of the jpg file
  });

  $('a').mouseleave(function(){
    $('img').hide();
  });
});
</script>

1 Answer 1

6

You can just concatenate the string for use, for example:

$("img[src='" + currentimg +"']").show();

Just to note, there's also a .hover() shortcut for .mouseenter(handler).mouseleave(handler), like this:

$(function(){
  $('img').hide();
  $('a').hover(function(){
     var currentimg = $(this).html();
     $("img[src='" + currentimg +"']").show();
  }, function(){
     $('img').hide();
  });
});
Sign up to request clarification or add additional context in comments.

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.