Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger Event on Text box Value change

I want execute the alert inside the $("#address").change function , but that needs to be done only if the the value is changed using the button .

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
   $('button').click(function(){
    $("#address").val("hi")
   })
   $("#address").change(function(){
    alert("The text has been changed.");
   });

});
</script>
</head>
<body>
<input type="text" id="address">
<button>Click</button>
</body>
</html>
like image 844
user3383301 Avatar asked Mar 26 '14 06:03

user3383301


People also ask

How do you trigger an event when a textbox is filled with value?

change function , but that needs to be done only if the the value is changed using the button . $("#address"). change will trigger if you perform any change in text field. If you want to trigger alert only when button click why you don't use alert inside button click function.

What event handler should be used to invoke a function on change of text in input field?

Try oninput : Unlike oninput , the onchange event handler is not necessarily called for each alteration to an element's value. Please Note: You also should not use function keyword as your function name.

Which event occurs when the value of an element has been changed?

The onchange event occurs when the value of an element has been changed.

Which event is triggered when a form field is changed?

Whenever the value of a form field changes, it fires a "change" event.


1 Answers

You can trigger change event in click function:

$('button').click(function(){
  $("#address").val("hi")
  $("#address").change(); //or $("#address").trigger("change");
});
$("#address").change(function(){
  alert("The text has been changed.");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="address" type="text">
<button>Change</button>
like image 97
Milind Anantwar Avatar answered Sep 23 '22 07:09

Milind Anantwar