Amit Dube
Amit Dube

Reputation: 1027

Javascript: Uncaught ReferenceError

<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"/>
<script type="text/javascript">
 $(document).ready(function(){
  $('#name').val('Name1');
 });
 function clickMe(){
  console.log('click me called');
 }
</script>
</head>
<body>
    Person Name: <input type="text" id="name" data-bind="value:personName">
    <input type="submit" value="Save" onClick="javascript:clickMe()"/>
</body>

</html>

In the code above neither the function inside document.ready is getting executed nor "clickMe" function is getting executed when "Save" button is clicked.

When I click on "Save" button, Uncaught ReferenceError: clickMe is not defined error message is seen.

Upvotes: 0

Views: 6814

Answers (3)

Minh Nguyen
Minh Nguyen

Reputation: 2191

You need to refer to script like this:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"></script>

Upvotes: 0

epascarello
epascarello

Reputation: 207501

Scripts can not be self closing. You need to have the closing </script> tag on the jQuery include.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"/>

needs to be

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"></script>

Upvotes: 1

Tushar
Tushar

Reputation: 87203

Because you haven't closed script tag of jQuery. <script> is not self-closing tag.

<script type="text/javascript" src="https://code.jquery.com/jquery- 1.11.3.js"/>

Should be

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"></script>

Code:

<html>

<head>
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $('#name').val('Name1');
    });

    function clickMe() {
      alert('click me called');
    }
  </script>
</head>

<body>
  Person Name:
  <input type="text" id="name" data-bind="value:personName">
  <input type="submit" value="Save" onClick="javascript:clickMe()" />
</body>

</html>

Upvotes: 2

Related Questions