0

I am trying to get the values of an element and then use it in another element within the same parent element using .each() function. However, this seems not to work. Here are the codes I am using. Please tell me where I am doing wrong.

HTML:

<table>
    <tr class="item">
        <td>
            <img class="thumbnail" src="http://www.domain.com/images/image1.jpg">
        </td>
        <td>
            <p class="img">http://www.domain.com/images/image001.jpg</p>
        </td>
    </tr>
    <tr class="item">
        <td>
            <img class="thumbnail" src="http://www.domain.com/images/image2.jpg">
        </td>
        <td>
            <p class="img">http://www.domain.com/images/image002.jpg</p>
        </td>
    </tr>
</table>

JavaScript:

<script type="text/javascript">
    $(document).ready(function() {
        $('.item').each(function() {
            $(this).find('.thumbnail').attr('src', $(this).find('.img').html());
        )}; 
    )};
</script>

What I want to do is get the html of the <p class="img"> (which contains the url to an image) and change the src of <img class="thumbnail"> with that on page load.

Please help

Abhik
  • 664
  • 3
  • 22
  • 40

5 Answers5

1

Fiddle

You had small typo error in your code. The closure of the functions }); was like )}; with inverted };

By the way .prop is preferred instead of .attr, you can read more here about it.

$(document).ready(function() {
        $('.item').each(function() {
            $(this).find('.thumbnail').attr('src', $(this).find('.img').html());
        }); 
   });
Community
  • 1
  • 1
Sergio
  • 28,539
  • 11
  • 85
  • 132
1
$(document).ready(function() {
     $('.item').each(function() {
         $(this).find('.thumbnail').prop('src', $(this).find('.img').html());
     }); 
});

At the end you had )}; instead of });. Also, .prop is preferred to .attr. quoting from the .prop documentation:

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.


It should be noted that in your specific situation, you are loading images after the page loads and after the jQuery fires. This causes a huge delay for the user. The thumbnails would load more quickly than a large image, but, at minimum, there is a flash of a broken image.
Mooseman
  • 18,763
  • 14
  • 70
  • 93
0

Typo error

<script type="text/javascript">
        $(document).ready(function () {
            $('.item').each(function () {
                $(this).find('.thumbnail').attr('src', $(this).find('.img').text());
            });
        });
    </script>
Manish Parmar
  • 859
  • 1
  • 11
  • 19
0

little error check at jsfiddle , use }); instead of )};

A.T.
  • 24,694
  • 8
  • 47
  • 65
0

Working code is:

$(document).ready(function () {
    $('.item').each(function () {
        $(this).find('.thumbnail').attr('src',$(this).find('.img').html());         
    })
});
Nono
  • 6,986
  • 4
  • 39
  • 39