Hacker Rocker
Hacker Rocker

Reputation: 251

Jquery change() not working

Hi everyone I am new to jquery. I am facing a problem with change function I jquery

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>

  $("input").change(function(){
    alert("The text has been changed.");
  });
</script>
</head>
<body>

<input type="text">
<p>Write something in the input field, and then press enter or click outside the field.</p>

</body>
</html>

I am not getting an alert box when I press it outside the textbox. Please help me to resolve this issue :D

Upvotes: 0

Views: 89

Answers (4)

RahulG
RahulG

Reputation: 1026

you can also try this :

    $(function() {
     $("input").change(function(){
            alert("The text has been changed.");
    });
 })

Upvotes: 2

bodruk
bodruk

Reputation: 3262

Put your code inside the:

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

The script will wait the DOM ready to start.

Upvotes: 0

Vipul
Vipul

Reputation: 325

you can also try this :

$(document).ready(function(){
$("input").change(function(){
    alert("The text has been changed.");
  });
});

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388436

You are trying to add a change handler to an element before it is added to the dom. You can use dom ready callback to delay the script execution till all the elements are loaded to the dom

jQuery(function($){
  $("input").change(function(){
    alert("The text has been changed.");
  });
})

Demo: Fiddle

Upvotes: 7

Related Questions