0

I have a partial view in ASP.NET MVC 3, with a script:

<script type="text/javascript" >
    $(function () {
        $("#tagbox").autocomplete({
            source: "/Tag/GetTags",
            minLength: 1,
            select: function (event, ui) {
                $("tagbox").val(ui.item.value);
            }
        });
    });
    </script>

when I load the patial view in my content div, the auto complete won't work, unless I remove the '$(function () {... }) so the script looks like this:

<script type="text/javascript" >
        $("#tagbox").autocomplete({
            source: "/Tag/GetTags",
            minLength: 1,
            select: function (event, ui) {
                $("tagbox").val(ui.item.value);
            }
        });
</script>

But when loading it as new view by accessing the URL, everything works just fine.

Also I have those references on my main view:

<script src="http://static.jsbin.com/js/prod/jsbin-3.4.4.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="../../Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

Second, when I change the order between the references, my ajax call opens as a new view and not as partial. Maybe one of them is unnecessary or something?

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Kobi Burnley
  • 105
  • 1
  • 2
  • 10
  • *"Second, ...*" changing the order could actually cause them to error due to missing dependencies, depending on what you change. As far as your main question, removing `$(function(){})` shouldn't magically make something start to work, though if it works without it, there's no reason to have it in the first place. It doesn't make sense to have `$(function(){})` in code that gets loaded in with ajax, though having it shouldn't affect whether or not the code runs successfully. – Kevin B Jul 31 '13 at 14:49
  • thanks for answering. Could you give me a short explanation on why it doesn't make sense to have $(function(){}) in code that gets loaded in with ajax? – Kobi Burnley Jul 31 '13 at 17:10
  • $(function(){}) executes function(){} after the DOM is ready. it's very unlikely that an ajax call would complete before the DOM is ready. – Kevin B Jul 31 '13 at 17:18

1 Answers1

0

Please follow the below link it will clearly explain about the jquery $(function(){}.

$(function(){}

Community
  • 1
  • 1
Selva
  • 527
  • 4
  • 14