Event binding for keyboard input or change through javascript - javascript

I have an input field with id myinput , and have change event bind to this field through,
$("input#myinput").change(function(){
alert("the value is changed");
}
But the handler is not getting called, if I change the value through javascript
$("input#myinput").val(text);
But, it works if I enter the input through keyboard.
so, it seems like I am binding to wrong event. What should I do to bind to both keyboard input and JS value update (jQuery val() here).
thanks.

change is triggered only by user events. With jQuery you can use trigger('change') to trigger the change event programatically.
JS Fiddle demo.

Related

How to handle change or input event of input type="text" if value is inserted from another element without touching this input

I have a input type="text" and next to that a button which opens up a calendar on click. When a value is selected in calendar textbox ix populated.
I would like to handle on change of textbox to enable other controls.
change, input etc events are triggered only if use manually enters values in the text box. But in my case I am not touching the input textbox.
Thanks.
Use jquery change event.
$("input").change(function(){
//do something
})
Update:
As .change event doesn't get fired when you change value using any javascript method, there is a work around.
When you change the date, you can trigger an event to the input element like $("input").keyup() and listen using:
$("input").keyup(function(){
// input updated
})
codepen example: https://codepen.io/azharuddinkhan8898/pen/MNrLad

Trigger event on Textinput or Textarea value change MeteorJS

I want add the contents of a textarea (or a textfield) to a reactive variable and bind it to a view, similar to AngularJS double binding.
The problem to listen to keydown is that the last character will not be included. If I use keyup there will be a delay. Is there a way to listen to value change in a text field and immediately after it changes set the reactive variable?
Template.body.events({
"keydown #textarea":function(){
input.set( $('#textarea').val());
}
});
You can use the HTML5 input event which is probably better suited for what you need.
Template.body.events({
"input #textarea":function(){
input.set( $('#textarea').val());
}
});

What event to know when user has entered data into form?

I have a HTML form. I want to enable/disable a button until user eneters text in one of the fields. I am adding an event attribute to the which triggers some javascript. This javascript will enable/disable the button.
Problem is I can't figure out what event attribute to use. What event attribute please will trigger as soon as user enters data? I tried onchange but that only gets called when i clicked back outside the text area. So it may aswell be onblur.
You can use the input
function activateForm (event) {
if(!this.value == ""){
}
}
var input = document.querySelector(".myInput");
input.addEventListener("input", activateForm , false)
There are 2 possible events that can be used: either onChange or onKeyPress. onChange will trigger when the value of an input has changed while onKeyPress will trigger every time the user types something in a text box. The onChange triggers once the user has CHANGED something in the value, and got out of the input focus. That means the user has to hit TAB or click somewhere else for the event to trigger, hence why onKeyPress might be better suited.
Read more:
http://www.w3schools.com/jsref/event_onchange.asp
http://www.w3schools.com/jsref/event_onkeypress.asp
Younger browsers also support onInput which should certainly be prefered for now, if you do not need to support older browsers.

onblur even is not working as expected

I have a form having 5 fields and a submit button.
On one of the fields i am having an onblur event but if the user changes the value in that textbox and directly clicks on submit button then in that case onblur is notgetting triggered or not working.
Even onchange is not working in that case.
What if i use onkeyup and down but if user used mouse for pasting the data?
Please help!
Thanks and regards
Asus.
You might want to try using the onchangeevent or oninput event to track when the input data is being modified. Then you can use the onsubmit event to call whatever function you would like.

How to make a real "onchange" event in HTML (no jQuery)?

Please note: I do not want to use jQuery for this (otherwise I like it)
Problem: I have come to situation where I need to do some javascript function after an user changes an input field (e.g. input type="text"). So I said to myself - I remember an onchange event - BUT onchange event runs AFTER user leaves the field, but I need the function to be called for example every time user types a character to that field. Wowhead shows nice example of what I am trying to achieve this (field with placeholder "Search within results...")
Summary: I am looking for a SIMPLE way of detecting REAL onchange event(not the classic HTML one)and through that call a JS function while not using jQuery?
Use onkeyup instead of onchange then.
Following is a simple way of invoking each type of Key Press on field.
input type="text" onkeypress="myFunction()
Get Example here
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeypress
Enjoy..
you can also use onkeyup and onkeydown instead of onkeypress.
Tried onkeypress="myFunction()" or onkeyup="myFunction()"?
There are also events for onfocus and onblur for entering and leaving a textfield :)
You should use "input" event. keyup and keypress don't work if a user modified the value only by a mouse.

Categories

Resources