Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - on change input text

I am trying to make a function which will execute when the value of a text input field is changed. The value of the field changes when a table cell is clicked, not on keyup, nor paste. I tried it like this:

$("#kat").change(function(){ 
    alert("Hello");
});

And I have the text field:

<input type="text" id="kat" value="0"/>

but it isn't working. Any help?

like image 243
Marko Cakic Avatar asked May 03 '12 19:05

Marko Cakic


People also ask

How do you detect change in text input box?

Answer: Use the input Event You can bind the input event to an input text box using on() method to detect any change in it. The following example will display the entered value when you type something inside the input field.

How do you find the change in input value?

The change() is an inbuilt method in jQuery that is used to detect the change in value of input fields. This method works only on the “<input>, <textarea> and <select>” elements. Parameter: It accepts an optional parameter “function”. Return Value: It returns the element with the modification.

How do you listen to input changes?

We can listen to input value change with JavaScript with the addEventListener method. Then we can listen for changes in the value inputted by writing: const input = document. querySelector('input') input.

How do I set the value of a element in jQuery?

jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.

How to set the value of input text in jQuery?

Example 1: In this example the value of the input element is set by val () method by selecting the input element from its ID. JQuery | Set value of input text. Example 2: In this example the value of the input element is set by val () method by selecting the input element from its parent element < body > and then selecting the < input > element.

How to change text inside an element using jQuery?

To change text inside an element using jQuery, use the text () method. You can try to run the following code to replace text inside an element:

How do I change the text on a click in jQuery?

Using the html() Method to Change Text on Click The jQuery html()method is very useful when it comes to manipulating web pages. We can use the jQuery html()method to change the html and text of an element. Let’s say we have the following code and we want to add html content to the span inside of our paragraph.

How to handle events on input text field in jQuery?

jQuery can be used to handle these events on an input text field. change (when text input field changes and loses focus) $ ('input#field1').on ('change', function (evt) { console.log (this.value) }); $ ('input#field1').change (function (evt) { console.log (this.value) });


2 Answers

This is from a comment on the jQuery documentation page:

In older, pre-HTML5 browsers, "keyup" is definitely what you're looking for.

In HTML5 there is a new event, "input", which behaves exactly like you seem to think "change" should have behaved - in that it fires as soon as a key is pressed to enter information into a form.

$('element').bind('input',function);

like image 70
aldux Avatar answered Oct 23 '22 15:10

aldux


Seems to me like you are updating the value of the text field in javascript. onchange event will be triggered only when you key-in data and tab out of the text field.

One workaround is to trigger the textbox change event when modifying the textbox value from the script. See below,

$("#kat").change(function(){ 
    alert("Hello");
});


$('<tab_cell>').click (function () {
   $('#kat')
      .val($(this).text()) //updating the value of the textbox 
      .change();           //trigger change event.
});
like image 9
Selvakumar Arumugam Avatar answered Oct 23 '22 17:10

Selvakumar Arumugam