3

The following code works on jsfiddle but does not work on localhost, can you help me please?

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script>
        $('.puankontrol').change(function() {
          var x = $(this).val() * 1;   
          if((x < 1) ||  (x > 5))
          {
              $(this).val(3);
              alert("Please enter value between 1-5!");
          }
        });
    </script>
</head>
<body>

     <form>
      <input class="puankontrol" type="text" name="puan"/><BR>
      <input class="puankontrol" type="text" name="puan"/><BR>
      <input class="puankontrol" type="text" name="puan"/><BR>
    </form>
    <div id="other">
      Trigger the handler
    </div>

</body>
</html>

`

Goran Mottram
  • 6,244
  • 26
  • 41
sdiri
  • 65
  • 5

2 Answers2

7

Try

$(document).ready(function(){
   //change function declaration.
});

also use <script type="text/javascript"> for adding script tag.

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
  • thank you so much for answer. after i posted i tried $(document).ready declaration and it worked. – sdiri Feb 25 '13 at 12:31
  • @sdiri always welcome dude..you should accept this answer... lol... :) already gave +1 for question dude.. – Dipesh Parmar Feb 25 '13 at 12:32
  • 1
    +1 from me. To add to Dipshe's answer. On JSFiddle.net it automatically wraps your code with "onLoad" by default (you can see it on the drop down box below "choose framework"). So probably why it works on jsfliddle and not locally. – Alex KeySmith Feb 25 '13 at 12:35
0

Whenever you want to write any code in jQuery, you must add that code in

$(document).ready(function(){});

jQuery calls this method callback once all elements of the documents are loaded. and it confirms that which all elements we are accessing inside this are loaded.

K D
  • 5,889
  • 1
  • 23
  • 35