Solved

Modify the src attribute of images after jQuery load via AJAX

Posted on 2014-10-16
2
431 Views
Last Modified: 2014-10-16
Here is a test page I am working on:
http://secure.bbdesign.com/shoperenowharton/country.htm

The big background images look like this:
<img class="rwdimage" data-rwdimage="ph_home_country_1.jpg" />

Then, this code populates the SRC attribute based on some Javascript Media Query logic:
$('div#homebg').children('img.rwdimage').each(function(){
	$(this).attr('src', 'imglo/'+$(this).attr('data-rwdimage'));
});

Open in new window


That part works great. However, there are also some images loaded via jQuery AJAX towards the bottom of the page (you would have to scroll down a bit to see them) that are not working. They are marked up the same way as the background image. Here is the script that handles them:
$('div#content').children('ul').children('li').children('img.rwdimage').each(function(){
	console.log('imglo/'+$(this).attr('data-rwdimage'));
	$(this).attr('src', 'imglo/'+$(this).attr('data-rwdimage'));
});

Open in new window


Note the console.log, if you view the console log you can see that the browser is picking up the "data-rwdimage" attribute and modifying it correctly. Yet, applying this value to the "src" attribute of the image does not work, as it does work on the non-AJAX loaded images.

I am guessing this has something to do with the AJAX-loaded elements, since that is the only way that they are different (unless I have made an error somewhere). However, since the script is seeing the "data-rwdimage" attribute, obviously it recognizes the page element in the DOM, why can I not update the SRC value?

Would appreciate any input.
Thank you!
0
Comment
Question by:bbdesign
2 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 40385060
It's actually setting the source attribute perfectly fine.
The problem is you have the class set as "rwdimage", which has height: 0 property on it.

The images are loading, they just have 0 height so you can't see them.
0
 

Author Comment

by:bbdesign
ID: 40385148
Ah, I see! Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now