Dirty Bird Design
Dirty Bird Design

Reputation: 5553

problem with live function

I had this working to spec, until the specs changed. This function is now brought in via ajax .load. Easy enough to bring it in and I have all my other functions on the page that is brought in working in the parent page except this one:

$("#CME").hide();
    $(function() {
        $("#CME1, #CMEQL, #CBT1, #CBTQL, #NYM1, #CMX1").live("change", function(){
            var checkBoxes =  $("#CME1, #CMEQL, #CBT1, #CBTQL, #NYM1, #CMX1").filter(":not(:checked)");
            if(checkBoxes.length == 0){
                $("#CME").slideDown("fast");
                } else {
                $("#CME").slideUp("fast");
                }
    });
});

the div "#CME" is not hidden and the .live('change', function () { isn't working. I have other similar .live functions that are working and structured the same. How do I bind the initial $(function() with .live and why isn't the .hide() working?

Here is the code that brings in this content

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

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

I think I may have just answered my own question

EDIT - I got it working by binding it (placing it in) to the .load function

Upvotes: 1

Views: 81

Answers (1)

gen_Eric
gen_Eric

Reputation: 227310

$("#CME").hide(); is not working because it is probably being run before the DOM is ready. Try placing that inside the $(function() {}); block.

.live() doesn't need to be inside the $(function() {}); block. live will bind an event whenever it appears in the DOM even if it doesn't exist when it's called.

Make sure your functions are closed. You seem to be missing a }); at the end of the code.

Upvotes: 3

Related Questions