1

I use jQuery to know my input changes .

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#id_back_ground_color").change(function(){
    alert("hello");
  });
    $("#salam").click(function(){
     alert("hey");
     $("#id_back_ground_color").val('changed!');
  }); 
});
</script>
</head>
<body>
<input id="id_back_ground_color" type="text">
<button id="salam" >

</body>
</html>

When I click on the button I get a "hey" alert but I don't get the "hello" alert. Why?

Markus
  • 3,225
  • 6
  • 35
  • 47
user3726821
  • 123
  • 1
  • 12
  • possible duplicate of [Why does the jquery change event not trigger when I set the value of a select using val()?](http://stackoverflow.com/questions/4672505/why-does-the-jquery-change-event-not-trigger-when-i-set-the-value-of-a-select-us) – JJJ Jun 28 '14 at 10:19

2 Answers2

0

There's no link between a click on the button and an edit in the id_back_ground_color field.

The change event is sent to an element when its value changes. This event is limited to elements, boxes and elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

From http://api.jquery.com/change/

Nicolas Henrard
  • 843
  • 8
  • 19
0

Change this line

 $("#id_back_ground_color").val('changed!');

to this

 $("#id_back_ground_color").val('changed!').trigger( "change" );
hex494D49
  • 9,109
  • 3
  • 38
  • 47