2

I call a section of another page in to the parent page like this:

$(function() {
    $("#subscribe").click(function () {
        $("#singleContent").load('purchaseForm.php #Content');
    });
});

no problem there. I am having an issue getting functions inside of 'purchaseForm.php' to run. I've tried the ".live" function, but can only bind that to events like click. I need to hide some things and get this code to run:

$('[class^=toggle-item]').hide();
$('[class^=link]').live('click', function(e) {
    $('.toggle-item-' + this.className).slideToggle("fast");
    $('span',this).text(function(i,txt) {
        return txt === "Learn More..." ? "Close" : "Learn More...";
        e.preventDefault();
    });
});

$(function() {
    var $add = $('input.add');
    $add.live('click', function() {
        $add.removeAttr('checked');
        $(this).attr('checked', true);
    });
});

How do I bind the above functions to the .load function?

2 Answers 2

2

You could do something like this:

$(function() {
    $("#subscribe").click(function () {
        $("#singleContent").load('purchaseForm.php #Content', function()
        {
            $('[class^=toggle-item]').hide();
            $('[class^=link]').live('click', function(e) {
                $('.toggle-item-' + this.className).slideToggle("fast");
                $('span',this).text(function(i,txt) {
                    return txt === "Learn More..." ? "Close" : "Learn More...";
                    e.preventDefault();
                });
            });

            $(function() {
               var $add = $('input.add');
               $add.live('click', function() {
                   $add.removeAttr('checked');
                   $(this).attr('checked', true);
               });
            });
        });
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

North Creative - perfect was not getting the structure correct. Would you recommend doing it this way or including all functions in an external file as Pointy "pointed" out?
Yeah, I would definitely externalize it wherever possible. It's a lot easier to maintain and track.
1

That's a jQuery bug I reported a while ago. To my knowledge, it's not fixed yet.

I'll find the bug and link it. Specifically, when you use ".load()" with a "context" like that (the selector following the URL), it does not run <script> tags in the content that's loaded.

edit — ok here is the bug.

To make your setup work, probably the best thing would be to have that code in the surrounding page that loads the content.

2 Comments

Well, a couple ways: first, I avoid doing that :-) Second, I try to make it so that all my pages have the same code available, as much as possible in external .js files.
good point. I should use one external js file for this set of pages that houses all the functions. thx man.

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.