Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a getJSON function that converts the results to an html template to display as a list that contains a different hyperlink within each list item. I need to override the normal html function of the link so it doesn't open a new page but loads into a specific divide using the load() method located within my navClickListener() function.

If I put a dummy url into var gotoURL = '';//<--..AND GET IT INTO THIS VAR..., it works as planned but I need to grab the actual url's from the array items.

I cannot figure a way to do this. Can you please look thru my snippets and pay attention to the inline comments in all CAPS.

Thank-you for your help.

function loadNav(url, container, appendE) {
        $.getJSON(url, function(data){

            $.each(data.items, function(){
                var newItem = $('#' + container).clone();
                // Now fill in the fields with the data
                . . . .
                newItem.find("a").attr("href", this.gotoURL);//<--PREVENT NORMAL FUNCTION...
                // And add the new list item to the page
                newItem.children().appendTo('#' + appendE)
            });
            . . . .
            // Click listener for navigation items
            var target = data.targetKey;
            var gotoURL = '';//<--..AND GET IT INTO THIS VAR...
            navClickListener(appendE, target, gotoURL);
        });
    };

    /* ========== Navigation click listener function ============= */   
    function navClickListener(appendE, target, gotoURL) {
        $('#' + appendE).on('click', 'a', function(e){
            e.preventDefault();
             $('#' + target).load(gotoURL);//<--...SO IT GETS INTO HERE??
             . . . .
        });
    };
share|improve this question

1 Answer

I'm not sure if I understand your question totally but can't you do something like this?

 function navClickListener(appendE, target, gotoURL) {
    $('#' + appendE).on('click', 'a', function(e){
        var myTarget = $(this).attr('href');
         $('#' + target).load(myTarget);
         return false;
    });
 };
share|improve this answer
Looks like a good start to me. You don't need to pass gotoURL. – Greg Pettit Apr 14 '12 at 5:32
e.preventDefault()? api.jquery.com/event.preventDefault – ANeves Apr 30 '12 at 7:00
Return false is similar to e.preventDefault() and e.stopPropagation(). It stops bubbling and the default event from happening...and it doesn't have to call another function to do it. – Dennis Rongo Apr 30 '12 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.