Copy title attribute value and paste as InnerHTML

I have Auto-Generated Code on our CMS site like this

<td class="photogalleryItem">
<a onclick="myLightbox.start(this);return false;" rel="lightbox[33136]" href="image1.jpg" title="Test">
<img border="0" src="image1.jpg" alt="Test">
</a>
</td>

      

I want to copy the href title or alt image value

and paste below:

<td class="photogalleryItem">
   <!--Like This <h3 style="text-align:center;">Test</h3>-->
    <a onclick="myLightbox.start(this);return false;" rel="lightbox[33136]" href="image1.jpg" title="Test">
    <img border="0" src="image1.jpg" alt="Test">
</a>
</td>

      

How can I do this when using javascript or JQuery.

This code can be found on the Gallery page. so this is repeated for all images

+3


source to share


2 answers


If you have one td

with a class photogalleryItem

, you can use:

$('.photogalleryItem').append('<h3 style="text-align:center;">'+ $('.photogalleryItem').find('img').attr('alt')+'</h3>');

      



If you have several td

with a class photogalleryItem

:

 $('.photogalleryItem').each(function(){
      $(this).append('<h3 style="text-align:center;">'+ $(this).find('img').attr('alt')+'</h3>');
 })

      

+2


source


Try this code



$('.photogalleryItem').each(function () {
    $(this).prepend('<h3 style="text-align:center;">' + $(this).find('img').attr('alt') + '</h3>');
});

      

+1


source







All Articles