Solved

How to get AJAX content loaded with jQuery to update

Posted on 2014-07-22
12
470 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Explain how this javascript function works 1 47
Problem to refer to value 8 49
Javasctipt 2 25
How to convert from xls to xlsx using java 7 39
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

777 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