David Ramírez
David Ramírez

Reputation: 23

$ is not defined (SharePoint + JS)

This error keep showing up, so hopefully you throw some light here, my code is the following:

<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js/"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js/"></script>
<script type="text/ecmascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/ecmascript" src="/_layouts/15/sp.js"></script>

<script>    
    function funcionX() {
        //$("#submit-button").click(function () {
            var context = SP.ClientContext.get_current();

            alert('Entro');

            var movies = context.get_web().get_lists().getByTitle("Movies");
            var movieCreationInfo = new SP.ListItemCreationInformation();
            var movie = movies.addItem(movieCreationInfo);
            movie.set_item("Title", $("#movie-title").val());
            movie.set_item("MovieDescription", $("#movie-description").val());
            movie.update();
            context.load(movie);

            context.executeQueryAsync(success, failure);


        function success() {
            $("#movies-output").text("Created movie!");
        };
        function failure() {
            $("#movies-output").text("Something failded");
        };
    };
</script>

Title: <input type="text" id="movie-title" /> <br />
Description: <input type="text" id="movie-description" /> <br />
<button type="button" id="submit-button" onclick="funcionX()">Add Movie</button>

<div id="movies-output"></div>

I understand the error is because my browser is not recognizing jquery, right?

Thank you for your help and sorry for the bad english!

Upvotes: 0

Views: 4411

Answers (2)

Raheel Shah
Raheel Shah

Reputation: 196

Try JQuery noConflict and should solve the issue.

<script> 
$.noConflict(); 
jQuery( document ).ready(function( $ ) { 
// Code that uses jQuery's $ can follow here. }); 
// Code that uses other library's $ can follow here.
</script>

Upvotes: 1

Ismail RBOUH
Ismail RBOUH

Reputation: 10470

Just remove the trailing slashs in your js libraries URLs:

<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js/"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js/"></script>

to:

<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>

Upvotes: 2

Related Questions