HTML input onchange event - javascript

I have an input box (FieldTest) on HTML page, and a javascript function that invoked when the value of the input box is changed.
When changing the value of the input box manually, then the function is triggered, but when I change that by jquery $("#FieldTest").val('Some Text'); Then the onchange function is not triggered!
Any Idea??

The onchange event is dispatched only if the user is typing in the input and the input loses focus.
To dispatch it manually, simply use this line:
$(“#FieldTest”).change();

Changing the value programmatically will not trigger an onChange event. You will have to create and dispatch one manually.

Related

Protractor: How to handle blur event in input textbox?

I am testing an app where we have written a directive which will attach blur event to input element,
Once user type some value and come out from textbox then it will check if value is not duplicate,
based on response it will set value using ctrl.$setValidity('unique', true);
When I am setting some unique to textbox using elem.sendKeys('newVal') and go to another input, it does not trigger blur event attached to the element.

How i get the keypress event of the input inside the select2?

I want to trigger an event when the input of a select2 search returns no results.
I want to trigger an event with the input value as a parameter for database search. Does anyone know how I can do this?
How can not capture the keypress event of the <input> within the select2 box?

How can I stop triggering the onChange event of a textbox in below mentioned scenario

How can I stop triggering the onChange event of a textbox in below mentioned scenario?
I have two text boxes on an aspx page;
If user change first text box value and its onChange event there is an asynchronous service call which updating the second textbox. During service call the control shifted on the second textbox. As the second text box value has been changed before it got focus, It triggers the second TextBox onChange event.

Select Element onChange event is not firing?

I have a select element with an onChange event that does fire when I click the select box and select a new value within it. But, when I tab to the select box and press the up or down arrows to change the select box's value, the event does not fire.
I am using jQuery().change(function(){ ... }); to set the event
When you tab into a <select> element, the change event doesn't fire until you press the Enter key.
For an onChange() to fire the value must be changed and the input must be blur()-ed (focus moved elsewhere); which is why it fires in your first case, but not in the second.
The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> 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.
Reference:
change().
As others have stated, the change event doesn't happen until the blur event. You'll need to monitor keyup as well to capture someone moving changing the values with the arrow keys.
Monitor keyup and change,
$('select').bind('change keyup', function() {
// Handle
});
http://jsfiddle.net/robert/Je26w/
You can trigger the blur event on keyup on the select and then give back focus, which will trigger the change:
$('select').keyup(function(){
$(this).blur().focus();
});
example: http://jsfiddle.net/niklasvh/XEg36/

can I use blur in a hidden field, jquery

can I use blur in a hidden field in jquery? if not, how can I track changes in a hidden field
Hidden field is hidden, it won't be visible to a user to change something. Only page author/script can change its value and if that is the case, you can use the change event to track changes.
$('#hidden_id').change(function(){
alert('Changed Value: ' + $(this).val());
});
NO, you can't in it's normal behavior.
The onchange event occurs when a control loses the input focus (blur) and its value has been modified since gaining focus.
http://www.w3.org/TR/html401/interact/scripts.html#adef-onchange
But you can do it as Sarfraz suggested. Add onchange event then explicitly trigger it every time you change the value of the hidden typed input
The 'change()' event handler doesn't work on hidden input fields. What you can do instead is something along these lines (untested!):
function hiddenOnChange(elem, function) {
if ($(elem).val()!=$(elem).getAttr('oldVal')) {
function;
}
$(elem).attr('oldVal', $(elem).val());
window.setTimeout(function(){hiddenOnchange(elem, function);}, 100);
}
Call this function with your hidden input field and the onchange function. It re-creates the onchange function by comparing the field's "current" value with a previously stored value. If nothing is changed, it re-checks in 100 msecs; If something did change, it will execute your function (I hope ;))
Hope this helps.

Categories