Karthikeyan
Karthikeyan

Reputation: 767

jquery delegate or live function does not working properly

when i try to implement a delegate function in jquery, it works perfectly when i use a alert(), inside but it doesnt get triggered when i remove that alert.

$(document).delegate( "#j_id2030916047_790d592c", "click", function(){ 

  alert("");
  $("#p4").hide();
  $("#p5").hide();
  $("#p3").hide();

  $("#check2").change(function(){

    if($("#check2").is(":checked")) {

      $("#p2").hide();
    }
    else{

     $("#p2").show();
  }
});

$("input[name='sor']").change(function(){

  if($("input[name='sor']:checked").val()=="collegeStudent"){

    $("#p3").show();
    $("#p4").hide();
    $("#p5").hide();
  }
  else if($("input[name='sor']:checked").val()=="schoolStudent"){

    $("#p4").show();
    $("#p5").hide();
    $("#p3").hide();
  }
  else if($("input[name='sor']:checked").val()=="employee"){

    $("#p5").show();
    $("#p3").hide();
    $("#p4").hide();
  } 
  else{

    $("#p5").hide();
    $("#p3").hide();
    $("#p4").hide();
  }
});

u can see a alert in line 2, all the functionalities works well if the alert is present.. when i remove the alert, nothing works..

Upvotes: 0

Views: 424

Answers (2)

ArVan
ArVan

Reputation: 4275

Put your function into $(document).ready, and it will work

$(document).ready(function(){
$(document).delegate( "#j_id2030916047_790d592c", "click", function(){ 

    //alert("");
        $("#p4").hide();
     $("#p5").hide();
     $("#p3").hide();
. . . 

   });
});

Upvotes: 0

Jake Feasel
Jake Feasel

Reputation: 16945

Are you putting this code within the document.ready event?

$(function () {  ....  });

It could be that your alert makes the difference because by that time, the document has loaded.

Upvotes: 2

Related Questions