Solved

How to get AJAX content loaded with jQuery to update

Posted on 2014-07-22
12
483 Views
Last Modified: 2014-07-24
I'm using rwd.images.js (https://github.com/stowball/rwd.images.js) to custom-load images of various resolutions, with markup that looks like this:

<img class="rwdimage" data-rwdimage="(max-width:768px){src:url(imglo/ph_estate-1.jpg);},(min-width:769px){src:url(img/ph_estate-1.jpg);}" />

Open in new window


However, when I load that markup with jQuery.load it doesn't work, presumably because I am adding content to the DOM and the JS isn't seeing it. My JS looks something like this:

$('div#content').load('details-'+$('div#homebg img:nth-child('+curImg+')').attr('data-link'));

Open in new window


What is the right way for me to do this? Somehow I need to tell rwd.images.js that new content has been added? There are instructions at the bottom of the URL above with regards to "lazy load" of images, but that doesn't seem to work, either. The AJAX-loaded content still has to be visible to JS in the first place, correct?

Thank you!
0
Comment
Question by:bbdesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40212045
This
$('div#content').load('details-'+$('div#homebg img:nth-child('+curImg+')').attr('data-link'));

should be
$('div#content').load('details-'+$('div#homebg img:nth-child('+curImg+')').data('link'));

Have you a link to the page? Confusing what the issue is without any real code.
0
 

Author Comment

by:bbdesign
ID: 40212140
The problem is not with the loading of content, that works fine. Here is a URL:
http://secure.bbdesign.com/shoperenowharton/hospitality.htm

The problem is that the images in the content that is loaded, which have class="rwdimage" like the large background images in the above URL, are not loading. I am presuming the reason is because of the AJAX load. I don't know how to get them to load.

If you monitor Firebug, for example, you will see this URL's content being inserted into the page:
GET http://secure.bbdesign.com/shoperenowharton/details-hospitality-1.htm

If I make them regular <img> tags with a src, they work fine. But since rdwimage does some dynamic loading of the src, that is where the problem is (I think).

Thanks.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40212215
Seems a bit amiss here - you are using $images then $image - which is never set.

console.log('load now');
var $images=document.getElementsByClassName('rwdimage');
var lazyLoad=function($image){
	$image.className+=' lazy-loaded';
	window.rwdImageChangeSrc($image);
};

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bbdesign
ID: 40212422
Hmm, not sure... I copied that script directly from the bottom of https://github.com/stowball/rwd.images.js

I tried making the variable names the same, but that seems to have no effect.

Anyway, I'm not 100% sure if I need that script or not. The images I am loading in the big background area are coded like this:

<img class="rwdimage" data-rwdimage="(max-width:768px){src:url(imglo/ph_home_hospitality_1.jpg);},(min-width:769px){src:url(img/ph_home_hospitality_1.jpg);}" data-category="Hospitality 1" data-link="hospitality-1.htm" alt="Hospitality 1" />

...and they work fine. But when I load in this via AJAX:

<img class="rwdimage" data-rwdimage="(max-width:768px){src:url(imglo/ph_det_hospitality-1a.jpg);},(min-width:769px){src:url(img/ph_det_hospitality-1a.jpg);}" data-rwdimage-lazy-load="true" />

...the image does not appear. So the only difference is the AJAX.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40212445
What should be happening with the other images - they are loading
Should they be appearing as you scroll down?
0
 

Author Comment

by:bbdesign
ID: 40212463
No, I'm not seeing the images. This is the URL:

http://secure.bbdesign.com/shoperenowharton/hospitality.htm

When I scroll down, that is where the images are supposed to be, but there is a blank space (the large, background images are working fine, but they are not loaded via AJAX).
1.jpg
0
 
LVL 58

Expert Comment

by:Gary
ID: 40212476
The only images I can see on the page are the background images.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40212480
Ok found them - will look at it
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40212614
Don't know without looking into the plugin and I don't debug other peoples plugins
Best to contact the author - because the code appears to be working fine.
That the images are loaded thru ajax shouldn't matter as you are calling the plugin after they have been loaded.
0
 

Author Comment

by:bbdesign
ID: 40212753
OK, thanks, I will continue to look into it.
0
 

Author Comment

by:bbdesign
ID: 40216424
I am loading the JS script before the later images, so probably that is why it isn't working?

I found the developer on Twitter today. He says he was already working on fixing the script to work with AJAX-loaded elements and will have a fix in a few days. Perfect!
0
 
LVL 58

Expert Comment

by:Gary
ID: 40216610
Good stuff.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
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…

734 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