Search code examples
jqueryhtmlimagealt

How to get img alt and add in other line jQuery


On page have many images with random alt=""
How to get current image alt on hover and add this alt here:

var img = jQuery('.bbc_img[alt="NEED_ADD_HERE"]').attr('src');

Sorry for my bad English language.


Solution

  • If I didn't get it wrong, you need the alt attribute on hover, so you can get the source image, right?

    $('img').on('mouseenter', function(e) {
        var alt = e.target.alt;
        $('#alt_hover').text(alt);
    
    
      var img = jQuery('.bbc_img[alt="'+alt+'"]').attr('src');
        $('#var_img').text(img);    
    });
    

    This will do the work, altought, you can obtain the same src directly:

    $('img').on('mouseenter', function(e) {
        var img = e.target.src;
        $('#var_img').text(img);    
    });
    

    Check the jsfiddle