can I use blur in a hidden field, jquery - javascript

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.

Related

HTML input onchange event

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.

how watch to change angularjs model which checkbox changed from jquery

I use https://github.com/Dimox/jQueryFormStyler for styling form inputs.
I set the property ng-model="c.model.acept".
If I click on "styled checkbox", then input gets to property "checked", but the angular model still has the old value.
I tried to trigger the change event with no result.
If I trigger the click event, then the checkbox gets changed twice (once from click, and then the second time from the triggered click event).
How to fix it?
You can not change "ng-model" of particular input field conditionally.
In this condition what you can do is, keep two input field and use "ng-show" or "ng-if" attribute to show & hide input field conditionally.
Hope this will hep you.

Update input value when using jQuery UI slider

I'm using the jQuery slider to change the value of an input.
div#slider
input#amount
I use the value of the input to perform some math with:
$("input#amount").change(function() {
...
The thing is, when I enter some value in the input, it works fine, but playing with the slider, the value appears to change, but no calculation is performed.
jsfiddle:
http://jsfiddle.net/mfeqoL7L/
Why/How to fix this?
Thx!
The problem is that setting a value on an input element using .val doesn't fire the change event: Why does the jquery change event not trigger when I set the value of a select using val()?
To manually fire the event call .change explicitly after setting the .val.

How to track change in textbox using jQuery

I want to check if user changes the value of a textbox. If the user changes the value, then I want to display the changed value in a different textbox.
jquery has an event for this, .change(), this event will detect content changes when the textbox looses focus.
If you are wanting to detect the change at every keypress, .keyup() and .keydown() might be better suited for your needs.
Examples for your intended use:
//just change '.change' to '.keyup' or '.keydown' the rest is the same.
$('input').change(function() {
$('target').val(this.value);
});

How to stop onChange in 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().

Categories