2

I apologize for my lack of jquery knowledge. I'm using jquery 1.6.2, and trying to attach a jquery function to an ActionLink. Here is my jquery function:

    <script type="text/javascript">
    $("addOperation").click(function () {
       $.ajax({
          url: this.href,
          cache: false,
          success: function (html) { $("#editor_rows").append(html); }
       });
       return false;
    });
    </script>

When I navigate to the page containing the script, the debugger comes back with the following:

    Microsoft JScript runtime error: '$' is undefined

This is pointing at the first occurence of '$'. To me, this sounds like it's not recognizing that this is a jquery call, but I haven't had trouble with this in the past. Not to mention, MVC3 should bundle and render all of the jscript files before the app loads. Any suggestions for me?

6
  • 3
    Is your jQuery script included before your script? Commented Jun 26, 2012 at 15:42
  • Check as first thing your html code to see if the jquery reference is before this script Commented Jun 26, 2012 at 15:42
  • Is this actually using JScript? Commented Jun 26, 2012 at 15:43
  • @Mathletics Microsoft always refers to it as JScript in their engine. Commented Jun 26, 2012 at 15:45
  • @vcsjones yes but it's also tagged jscript, which is probably not accurate. Editing. Commented Jun 26, 2012 at 15:46

1 Answer 1

3
  1. Make sure your jquery script tag is above your script.

  2. Wrap your code in the document.ready event:

E.g. for 1

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="/path/to/your.script"></script>

E.g. for 2

$(document).ready(function() { 
     //Your code here
});

or shorter:

$(function() { 
       //Your code here
});

This will ensure all the JavaScript has loaded before your code is executed.

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

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.