Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Jquery apply jquery to elements after Ajax load

Posted on 2010-08-22
3
Medium Priority
?
383 Views
Last Modified: 2013-11-11
Hi I have the following fragment of code, Once the content has been loaded the people_content function is called, but it does not apply the hide function to the elements called tabs. I suspect it is because it being loaded by ajax.
Any clues?

$('.choose_people').click(function(e){
    person_id=$(this).val();
    url=$(this).parent().attr('action');
    $('#people_content').load(url+'/'+person_id+'/modal',function(){
        ajaxloadergif(true);
    });
        ajaxloadergif(false);
        
    e.preventDefault();
    people_content();
})

function people_content(){
    $('#people_content','.tabs').hide();
}

Open in new window

0
Comment
Question by:jdav357
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1600 total points
ID: 33497552
What about :


// wait end of ajax call before calling people_content
	$('.choose_people').click(function(e){
		person_id=$(this).val();
		url=$(this).parent().attr('action');
		$('#people_content').load(url+'/'+person_id+'/modal',function() {
			ajaxloadergif(true);
			people_content();
		});
		ajaxloadergif(false);	
		e.preventDefault();
	})
	
	function people_content(){
		$('#people_content','.tabs').hide();
	}

Open in new window

0
 
LVL 15

Assisted Solution

by:SRigney
SRigney earned 400 total points
ID: 33502392
leakim971 has it right, you need to use the callback function that waits until the page is complete before calling the people_content function.

You could also just call the mehtod.  Another method would be to set the style of that html to hidden when it's loaded.  That may prevent a flicker that will occur from time to time if you rely completely on jQuery to hide the section.
'.choose_people').click(function(e){
		person_id=$(this).val();
		url=$(this).parent().attr('action');
		$('#people_content').load(url+'/'+person_id+'/modal',function() {
			// the load method is complete.
                        ajaxloadergif(true);
			$('#people_content','.tabs').hide();
		});
		ajaxloadergif(false);	
		e.preventDefault();
	})

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33509462
Thanks for the points!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

972 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