0

How do I select objects and apply functions to them if they are loaded in with Ajax?

This code works, but I would like this code to run without the click event. So it runs automatically.

$(document).on("click",".button",function(e){

    var imgcount = $('.slider img').length;
    var slidesize = 100 / imgcount
    var innersize = 100 * imgcount

    $('.slider img').wrap('<div class="slide" />');
    $('.slide').wrapAll('<div class="inner" />');
    $('.inner').wrapAll('<div class="overflow" />');
    $('.slide').css('width', slidesize + '%');
    $('.inner').css('width', innersize + '%');

    });

This is how I load in my content

$('.work-item-hover').click(function(){

    var toLoad = $(this).attr('href')+' #content-post > *';
    $('#content-post').slideUp('normal',loadContent);
    $('#load').remove();
    $('.content-post-wrap').append('<span id="load">LOADING...</span>');
    $('#load').fadeIn('normal');
    window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
    function loadContent() {
        $('#content-post').load(toLoad,'',showNewContent())
    }
    function showNewContent() {
        $(document).ajaxComplete(function(){
    $('#content-post').slideDown('slow',hideLoader());
});
5
  • I do not see where ajax comes into play here, please elaborate. Else $(document).ready(...) might be your simplest solution. Commented Jun 1, 2013 at 19:04
  • All of these elements (slider img, slide, inner) they are all loading in with ajax. Commented Jun 1, 2013 at 20:05
  • Maybe instead of onclick register this function with (or execute it from) the success method of your ajax call? Commented Jun 1, 2013 at 20:11
  • I updated the post with the load in call to show you where I'm loading in the content. Commented Jun 1, 2013 at 20:12
  • Can you explain a bit more how I would register it with the success? I'm new to jQuery. Trying to learn :) Commented Jun 1, 2013 at 20:14

3 Answers 3

1

Trigger the click event explicitly

    $('.slide').css('width', slidesize + '%');
    $('.inner').css('width', innersize + '%');
}).click();
Sign up to request clarification or add additional context in comments.

Comments

0

In your case the showNewContent() is your success method. You could insert your code there (or wrap it in a another named function for convenience).

function showNewContent() {       

    var imgcount = $('.slider img').length;
    var slidesize = 100 / imgcount
    var innersize = 100 * imgcount

    $('.slider img').wrap('<div class="slide" />');
    $('.slide').wrapAll('<div class="inner" />');
    $('.inner').wrapAll('<div class="overflow" />');
    $('.slide').css('width', slidesize + '%');
    $('.inner').css('width', innersize + '%');

    $('#content-post').slideDown('slow',hideLoader());
}

Note that I removed the call to .ajaxComplete() as it is imho redundant since showNewContent() is registered to .load() as the complete handler here .load(toLoad,'',showNewContent()) (and only for this single request). With your original call you register for all ajax requests.

sandboxed test

Comments

0

$(document).ready(function(){}); - other than that, make sure your call $('element').click(); in your success function of any AJAX request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.