How to stop onChange in JavaScript - javascript

In one of my selection boxes, I have an onChange="..." specified...
because I want to change some other form value after any selection changes.
However, in the same page, some weird case I have to manually set the value.
So I have to use some JavaScript to set the value of the selection combobox, but in this case, I don't want that onChange event to be fired.
How can I walk around it?
Forgot to mention that I am actually using dijit.form.comboBox.
For normal HTML form comboBox, it won't cause any issue.
Only I use the dijit comboBox, and I try to set the value to some other value, dojo will trigger the onChange.

If you are using Dijit, then you can pass an additional false flag at the end of the set() method that will prevent the widget from firing the onChange event.
For example:
dijit.byId(myComboBox).set("value","Choose an option...",false);

Found this answer from Paul Christopher at http://dojo-toolkit.33424.n3.nabble.com/onchange-event-firing-when-setting-value-of-a-Select-programmatically-td3985692.html. It worked perfectly!
myDigit._lastValueReported = myValue;
myDigit.set('value', myValue);

You don't need to do anything. Setting the value with Javascript will not fire your onchange event handler.

In general, setting the value with JavaScript won't fire onchange. If you're dealing with a strange browser that does fire it, you could remove the onChange (element.onchange = null), change the value, then add it back (element.onchange = functionname) afterwards.

FYI, this answer is not fully correct. It is true that simply setting the value does not trigger the onChange event, BUT as soon as the control loses focus, the change will be detected and onChange will be fired.
So delaying onChange is not really the same as preventing onChange - which is what I need to do!
I could temporarily remove the event, blur and refocus the field, and then restore the event, but this is an ugly hack. It is complicated by dynamicaly added events like jQuery. so really what I'd like is to set the 'focus value' to the 'new value', but haven't been able to find this. I could try setting the defaultValue, but this would prevent a correct form.reset().

Related

change event not firing after setting value dynamically

I want to bind change event to textarea(read only) whenever its value is set dynamically by opening popup window.
I am able to set the value, but the change event is not getting fired.
I used below code to bind change event to textarea :
$('textarea[name="Cordinator"]').bind("change", onChangeCordinator);
function onChangeCordinator(){}
How are you setting the value? By default the change event fires only if the value is changed by the browser user.
If you are setting the value programatically you need to use .trigger('change')
So somewhere in your onclick handler you need:
$('textarea[name="Cordinator"]').trigger('change');
there is a syntax error in your js
change this to
$('textarea[name="Cordinator"]').bind("change", onChangeCordinator);});
this
$('textarea[name="Cordinator"]').bind("change", onChangeCordinator);
UPDATE:
well you need to trigger it manually after setting the value on textarea like this
$('textarea[name="Cordinator"]').val('Set Your Value Here').trigger('change');
DEMO

onchange wont fire after programmatically changing html select dropdown

I have a select inside HTML
<select id="league" name="league">
which I'm listening for changes inside my javascript.
var league = dojo.byId("league");
dojo.connect(league, "onchange", function (evt) { //do stuff }
Which works fine.
However I have a link that I can click which updates the select:
League
The link works as it updates the selected value of the select with the following function.
function updateSelection(NewLeague){
dojo.byId('league').value = NewLeague; // works
dojo.byId('league').onChange; //this isnt working
//dojo.byId('league').onChange(); //this throws: TypeError: dojo.byId("league").onChange is not a function
}
My problem, as I've read through other stack posts is that programmatically updating the value wont trigger onChange, thus I need to call onchange in the code (shown above). As per the comments inline, the onChange isn't being triggered or throws an error. My first thought that it has something to do with the dojo.Connect which listens for onChange, but I havent found any information that says I cant do this, nor any explanation how to get around it.
Any ideas?
Select onchange doesn't fire for programattic changes, you need to fire it yourself with league.onchange();
As noted by #Greg, the call should be lowercase.
Additionally, I don't know if dojo has a trigger method, but in jQuery this would be done as jQuery('#league').trigger('change').
Depending on your version of dojo you may also want to check: http://dojotoolkit.org/reference-guide/1.8/dojo/connect.html
Have you tried just calling the select by it's id using normal js?
document.getElementById('league').onchange.call();
As others have said, you need to trigger the event yourself, just setting the value does not do that. See the code on How to trigger event in JavaScript? to see how in a cross-browser way.

Detect change of select list using Jquery

Is there a way to detect when the value of a select list is set to empty by a javasscript and not by the user? It seems that the change-event only triggers by mouse or keyboard.
And is there a way to detect when the number of options in a select list changes (added, removed)?
You have to trigger the change event manually, when you are changing the value of a select with javascript. E.g:
$('#myselect').val(10).change();
In this example the value is set to 10 and the change event is triggered. If there is an event handler attached to the select, it will be executed.
Use Jquery's change function
$("#idofselect").change(function(){ });
To answer your first question, not it's not possible to detect what caused the change in the select list in the change event itself.
However, if there is javascript code changing the select list you could add some logic in there to perform the tasks needed in this scenario.

How to know when a textbox changes (without waiting for blur)?

I have an <input> textbox.
I wish to call a function when it has been modified. The default behavior of the change event is to only trigger on blur, but I need to call my function as soon as the textbox's text changes and can't wait for blur.
I'm not sure the best way to do this. I thought of maybe handling keydown and then testing the value of the keyCode for a key that would modify the value. So... ignore arrow keys, etc. But that seems awkward and I don't think I can figure out the test to make it work.
I also thought about recording the inital value and comparing to this value but then I wouldn't be notified in the case when the value changes back to the original.
Maybe there's a better way?
I'd appreciate any suggestions.
a combination of focus and keyup is probably your best bet. record the initial value in the focus handler, and check against the current value in the keyup handler. (the new input value, if any, is available when keyup fires).
You can use the keypress function.
You can use the change() function

onchange event from select box to update field

On this page:
http://www.blackdownluxurylettings.co.uk/place_booking/2010-3-18,2
I am using an onchange event on the "number of days" select box to update the "departure date" field.
But currently nothing is happening.
Any ideas?
Just looking at it quickly: wouldn't you want the ONCHANGE event attached to the SELECT tag rather than the individual options?
You can bind it this way using JQuery:
$("#ddlNumberOfDays").bind('change', mainPharmacy_Change)
Maybe for javascript, you should just try 'change'.
If I recall correctly, the regular Javascript onChange event only fires once the select box loses focus AND it's contents have been changed. I don't know how jQuery achieves it, but their change method will fire whenever the contents are updated.
Also, I get the following error in my Javascript console when loading your page:
[cycle] terminating; zero elements found by selector

Categories