jquery in sharepoint 2013 web part

AdamBT picture AdamBT · Apr 16, 2014 · Viewed 7.3k times · Source

I am trying to add Jquery to a SharePoint 2013 Visual webpart. I have put the script files in the layouts folder and I am able to reference them in my ascx, but it is not recognizing any jquery syntax.

<script src="../_layouts/15/jquery-2.1.0.min.js"></script>
<script src="../_layouts/15/jquery-ui-1.10.4.min.js"></script>


<script>
    $(function () { $("#accordian").accordion(); });//not recognized
</script>

Any advice would be appreciated.

Thanks

EDIT

When I try running it, I get Error: 'jQuery' is undefined. When I debug, it is in the UI script file, so it looks like it is loading the jQuery-UI but not the main jQuery script. (Maybe?)

EDIT

Actual problem turned out to be my jQuery file was malformed. @Moderator - Please delete

Answer

c0deNinja picture c0deNinja · Apr 16, 2014

You might want to put your jQuery files in a specific folder in Layouts as to not clutter the Layouts directory.

And then you can simply reference your jQuery files like this:

<SharePoint:Scriptlink ID="jQuery" runat="server" Name="MyWebPart/jquery-2.1.0.min.js" Language="javascript" />
<SharePoint:Scriptlink ID="jQueryUI" runat="server" Name="MyWebPart/jquery-ui-1.10.4.min.js" Language="javascript" />

And so your files would be sitting in the following location on the server:

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\MyWebPart\jquery-2.1.0.min.js

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\MyWebPart\jquery-ui-1.10.4.min.js