Solved

Modify the src attribute of images after jQuery load via AJAX

Posted on 2014-10-16
2
422 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
Comment Utility
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
Comment Utility
Ah, I see! Thank you!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
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…

743 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

17 Experts available now in Live!

Get 1:1 Help Now