'How to make every image title equal to alt text?

I need onhover-titles for all images - in products, in product category, etc. In wp media library all images have their alt text, I need to show them as a title of each image. Is there a hook to do it? I try to do it by jQuery

add_action('wp_footer', 'product_alt_title');
function product_alt_title(){
global $post;
 $image_id = get_post_thumbnail_id();
 $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);                        
?>
    <script>
         var image_alt =  <?php echo $image_alt ?>; 
         jQuery(document).ready(function($){
            jQuery('img').attr('title','image_alt ');
        });
    </script>
<?php }

But console doesn't see my var value, it shows like var image_alt = ;
It's a shop, not an article, so I exactly need the code decision. What is the right way?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source