-1

I'm looking for a function to load content into a page. If I use it this way works fine

$(document).ready(function() {

    $('#nav li a').click(function(){                      
        var toLoad = $(this).attr('href')+' #content';
        $('#load').remove();
        $('#wrapper').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('normal',loadContent);
        function loadContent() {
            $('#content').append($('<div>').load(toLoad,'',showNewContent()))
        }
        function showNewContent() {
            $('#content').fadeIn('normal',hideLoader());
        }
        function hideLoader() {
            $('#load').fadeOut('normal');
        }
        return false;   
    });
});

but when I want to make it function

jQuery.addc = function(){

        var toLoad = $(this).attr('href')+' #content';
        $('#load').remove();
        $('#wrapper').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('normal',loadContent);
        function loadContent() {
            $('#content').append($('<div>').load(toLoad,'',showNewContent()))
        }
        function showNewContent() {
            $('#content').fadeIn('normal',hideLoader());
        }
        function hideLoader() {
            $('#load').fadeOut('normal');
        }
        return false;   
};

stop working, when I call the function with onclick

<ul id="nav">

        <li a href="index.html" onclick="$.addc();"> Index </a> </li>
</ul> 

I don't know whats happening because first code works fine, but second don't, If anyone can help me please I also try $.fn.addc = function () but don't work

1
  • Thanks to everyone, I've modified the script, now works better, thanks Commented Apr 18, 2011 at 18:55

5 Answers 5

1

Extract only the innards into a function:

$(document).ready(function() {
    $('#nav li a').click($.addc);
}); 

$.addc = function() {                    
    var toLoad = $(this).attr('href') + ' #content';

    $('#load').remove();
    $('#wrapper').append('<span id="load">LOADING...</span>');
    $('#load').fadeIn('normal',loadContent);

    function loadContent() {
        $('#content').append($('<div>').load(toLoad,'',showNewContent()))
    }

    function showNewContent() {
        $('#content').fadeIn('normal',hideLoader());
    }

    function hideLoader() {
        $('#load').fadeOut('normal');
    }

    return false;   
};
Sign up to request clarification or add additional context in comments.

Comments

0

the whole idea (or at least a main one) is to remove inline js.

stick to your first solution,

or what you can do is:

var clicked = function(){
        var toLoad = $(this).attr('href')+' #content';
        $('#load').remove();
        $('#wrapper').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('normal',loadContent);
        function loadContent() {
            $('#content').append($('<div>').load(toLoad,'',showNewContent()))
        }
        function showNewContent() {
            $('#content').fadeIn('normal',hideLoader());
        }
        function hideLoader() {
            $('#load').fadeOut('normal');
        }
        return false; 
    }

$('#nav li a').live('click', clicked);  

Comments

0

This is because the context of this is changing. this in your first code snipped corresponds to the element of your selector; one of the elements in #nav li a. In your second snippet, this does not reference to same thing, so it doesnt work.

In addition, it's bad form to attach a "static" function to the jQuery object when it is specific to an implementation of yours. You should extract that logic into a method, and then call that like so:

onclick="myMethod();"

Comments

0

Your context is all wrong. In the original, you have:

$(document), so this will be the document (or maybe the jQuery wrapper for the document). You will need to set the context.

For example, create a new function:

function aoeu () {
    $.fn.addc.call(document);
}

I would change the $(this) to $(document) instead. This will make your life a whole lot easier.

Comments

0

You are using this, which in the first case was passed in by jQuery, but in the second case it is up to you to pass it in.

e.g. onclick="addc(this);" and give your function a signature of function addc(this) {...}

(there is not need to overload the jQuery namespace for a custom and very specific function)

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.