1

I have added some javascript function in my partial page. But it is not firing. Need help.

Thanks in advance

@section scripts {
<script type="text/javascript">
$(document).ready(function () {


    $('#btn').click(function (event) {
        alert("Hi");
        var filterstring = $('#txtCode').val();
        $.get("/Test/Details?filterstring=" + $('#txtCode').val() + "", function (data) {
            var getHTML = $(data);
            $('#WebGrid').empty();
            $('#WebGrid').html($('#WebGrid', getHTML));
            ChkAddClass();
        });
    });


</script>
   }
8
  • 2
    put it in main view and check Commented Jul 24, 2014 at 11:52
  • Are you loding partial view via AJAX ? Commented Jul 24, 2014 at 11:55
  • yes using @Ajax.ActionLink() @Saranga Commented Jul 24, 2014 at 11:56
  • It is not working. I tried it before @Kartikeya Commented Jul 24, 2014 at 11:57
  • @Shariful_Islam have you check in browser? is there any error you get? Commented Jul 24, 2014 at 12:03

2 Answers 2

5

It is not possible to use sections in a partial view, you have to move this script to main view or use a custom helper.

And also since you are using AJAX you have to use delegated events to attach an event handler.

Please read the Direct and delegated events section of .on().

$(document).on('click', '#btn', function(){
    alert("Hi");
    var filterstring = $('#txtCode').val();
    $.get("/Test/Details?filterstring=" + $('#txtCode').val() + "", function (data) {
        var getHTML = $(data);
        $('#WebGrid').empty();
        $('#WebGrid').html($('#WebGrid', getHTML));
        ChkAddClass();
    });
});

Thanks!

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it like that. But its not working. Even in page source I did not find my javascript.
It is not possible to use sections in a partial view, I updated my answer.
2

You are closing you doc ready block after the closing script tag and you are missing );

<script type="text/javascript">
$(document).ready(function () {


    $('#btn').click(function (event) {
        alert("Hi");
        var filterstring = $('#txtCode').val();
        $.get("/Test/Details?filterstring=" + $('#txtCode').val() + "", function (data) {
            var getHTML = $(data);
            $('#WebGrid').empty();
            $('#WebGrid').html($('#WebGrid', getHTML));
            ChkAddClass();
        });
    });

});
</script>

Comments

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.