Edison Liu
Edison Liu

Reputation: 1

html form add some javascript code

i want to add javascript in my html form modal. but i got error "uncaught referenceError: $ is not defined" when i define count variable.

So ,how can i resolve this question.

<div class="modal">
    <form id="multiform">
        <script rel="javascript" type="text/javascript" href="/static/js/jquery.min.js">
            var count=$('select#ip option:selected').length;
            alert(count);
            /*code more*/ 
        </script>
    </form>
</div>

Upvotes: 0

Views: 38

Answers (2)

gurvinder372
gurvinder372

Reputation: 68433

There are 3 mistakes

  • Use src instead of href.
  • Once you have given src to a script tag, then script inside that tag will be ignored.
  • Keep it outside body tag or in head tag.

Demo

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal">
  <form id="multiform">
  </form>
</div>
<script>
  var count = $('select#ip option:selected').length;
  alert(count);
</script>

Upvotes: 3

Rahul Sahu
Rahul Sahu

Reputation: 235

Try this. you are using href insted of src

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal">
    <form id="multiform">
        <script  type="text/javascript">
            var count=$('select#ip option:selected').length;
            alert(count);
            /*code more*/ 
        </script>
    </form>
</div>

Upvotes: 0

Related Questions