Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Listen For All Text Input Changes

I have an input element within a form which I would like to be able to extract its value when the value changes. Take a look at this Fiddle: http://jsfiddle.net/spryno724/WaBzW/1/.

By using the change event, I am able to display the value from the text input when the input looses focus. However, the value does not update when the form is reset or when JavaScript clears the value of the text input.

Is there a specific event that I can listen for which will be dispatched when any change is made to a text input control?

I'd like to avoid work-arounds, such as listening for when the form is reset, or when the clear button is pressed. This is a simplified example of what my application does, and it will get crazy pretty fast if I try to do all of that.

Thank you for your time.

like image 960
Oliver Spryn Avatar asked May 28 '12 03:05

Oliver Spryn


People also ask

How to detect change in input text jQuery?

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.

How to check if textbox value is changed in jQuery?

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.

How to detect change in input text JavaScript?

Use any or all of them onchange event, onpropertychange event, onkeyup event, onpaste event and oninput event in the input element and call a function to see the effect. click outside of it to see.

Which input event will you use on an input tag to get notified every time a user changes something?

The oninput event occurs when an element gets user input. This event occurs when the value of an <input> or <textarea> element is changed.


3 Answers

$(document).ready(function() {
    $('input.clear').click(function() {
        $('input.input').val('');
        $('p.display').text('The value of the text input is: ');
    });

    $('input.input').on('keyup change', function() {
       $('p.display').text('The value of the text input is: ' + $(this).val());
    });
})​

DEMO 1

Probably this solution may help you:

$(document).ready(function() {
    $('input.clear, input[type=reset]').on('click', function() {
        $('input.input').val('').change();
        $('p.display').text('The value of the text input is: ');
    });

    $('input.input').on('keyup change', function() {
        $('p.display').text('The value of the text input is: ' + $(this).val());
    });
});​

DEMO 2

like image 65
thecodeparadox Avatar answered Sep 29 '22 03:09

thecodeparadox


This question is an exact duplicate of another. See the answer with the highest number of up-votes as the answer to my question:

JS Events: hooking on value change event on text inputs

like image 34
Oliver Spryn Avatar answered Sep 29 '22 03:09

Oliver Spryn


Override val method of jQuery

HTML

<input id='myInput' name='myInputField' value='v1'>

JS

var myInput = $('#myInput');

//this will not trigger the change event
myInput.val('v2');

//override val method
var oldVal = myInput.val;
myInput.val = function(value){
   var returnVal = oldVal.call(this,value);
   myInput.change();
   return returnVal;
}

// this will trigger the change event
// myInput.val is overridden
myInput.val('v3');

NOTE this will work only if val method is called from myInput variable

like image 24
Raaghu Avatar answered Sep 29 '22 02:09

Raaghu