0

my Jquery code works fine if i dont keep the code inside function and call it but as a function not working

<script>
    $(document).ready(function(){
        $("input:radio").change(function(){
        checkResult();
        });
    });
        function checkResult()
        {
        $this=$(this).parent("div.QA");
        $this.slideUp();
        }

</script>
TaraGurung
  • 135
  • 1
  • 21

2 Answers2

4

because this inside the function does not refer to the clicked radio element, since you are writing it as a separate function pass the clicked element as a parameter

$(document).ready(function(){
    $("input:radio").change(function(){
        checkResult(this);
    });
});
function checkResult(el) {
    var $this = $(el).parent("div.QA");
    $this.slideUp();
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Pass the function name - checkResult as callback, so that this will refer to the changed element.

$(document).ready(function(){
    $("input:radio").change(checkResult);
});
function checkResult()
{
    $this=$(this).parent("div.QA");
    $this.slideUp();
}
Jithesh
  • 972
  • 4
  • 10