318

I have a hidden text field whose value gets updated via an AJAX response.

<input type="hidden" value="" name="userid" id="useid" />

When this value changes, I would like to fire an AJAX request. Can anyone advise on how to detect the change?

I have the following code, but do not know how to look for the value:

$('#userid').change( function() {  
    alert('Change!'); 
}) 
3
  • 2
    If it gets updated via an ajax response, why don't you just fire the new ajax request in the success function of the response? Commented Jun 30, 2011 at 10:02
  • 2
    $('#userid').val() will give you the value if that's what you're asking Commented Jun 30, 2011 at 10:03
  • UPDATE: a change event is now triggered when the value of a hidden field is updated. Commented May 4, 2016 at 21:58

9 Answers 9

703

So this is way late, but I've discovered an answer, in case it becomes useful to anyone who comes across this thread.

Changes in value to hidden elements don't automatically fire the .change() event. So, wherever it is that you're setting that value, you also have to tell jQuery to trigger it.

function setUserID(myValue) {
     $('#userid').val(myValue)
                 .trigger('change');
}

Once that's the case,

$('#userid').change(function(){
      //fire your ajax call  
})

should work as expected.

Sign up to request clarification or add additional context in comments.

6 Comments

Is .trigger() generally better to use over just calling change()?
It works, but it seems like it triggers the change event twice!. Is like if I put this code, it treggers it twice, if I remove the code, no triggers w/e.!!
To make it behaves the same as change event you should add the code in setUserID() to check whether the the value is really change or not. if ($('#userid').val() != myVaule) { // set val() and trigger change }
This is especially useful if you are are trying to catch a "change" event that as being changed via javascript, otherwise the changes made by javascript cannot be caught by the .change(handler) or .on("change", handler)
so If I don't have control on the function that changes the value of the hidden field (i.e. can't fire the trigger), this solution wont work , right ?
|
55

Since hidden input does not trigger "change" event on change, I used MutationObserver to trigger this instead.

(Sometimes hidden input value changes are done by some other scripts you can't modify)

This does not work in IE10 and below

MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

var trackChange = function(element) {
  var observer = new MutationObserver(function(mutations, observer) {
    if(mutations[0].attributeName == "value") {
        $(element).trigger("change");
    }
  });
  observer.observe(element, {
    attributes: true
  });
}

// Just pass an element to the function to start tracking
trackChange( $("input[name=foo]")[0] );

6 Comments

AFAIK the mutationobserver also does NOT get fired on a change inside a text field (no matter if hidden or not)
Works great! @OleAlbers - OP asked about input[type=hidden]
I've used this in combination with a function that was using the map command of jquery to retrieve a collection of hidden input field values. Thanks so much @lulalala!
Simple and easy to understand. This solution, as opposed to the "you must trigger the event when you change the value" solutions, does not rely on the programmer having access to the code at the point where the value is changed, making it more useful as long as you don't have to support a version older than IE11. It worked great for me. Thanks
Great solution! Thank you! For some reason the trigger('change') didn't work for me, so I've created a CustomEvent, registered it and then element.dispatchEvent(myCustomEvent)
|
9

You can simply use the below function, You can also change the type element.

 $("input[type=hidden]").bind("change", function() {
       alert($(this).val()); 
 });

Changes in value to hidden elements don't automatically fire the .change() event. So, wherever it is that you're setting that value, you also have to tell jQuery to trigger it.

HTML

 <div id="message"></div>
<input type="hidden" id="testChange" value="0"  />    

JAVASCRIPT

var $message = $('#message');
var $testChange = $('#testChange');
var i = 1;

function updateChange() {
    $message.html($message.html() + '<p>Changed to ' + $testChange.val() + '</p>');
}

$testChange.on('change', updateChange);

setInterval(function() {
    $testChange.val(++i).trigger('change');; 
    console.log("value changed" +$testChange.val());
}, 3000);

updateChange();

should work as expected.

http://jsfiddle.net/7CM6k/3/

6 Comments

Not working for me... Anyway, how could it be possible to paste value into an Hidden Field? :/
Hey Thanks, paste should not be there, but change can detect the hidden field change event
@KrisErickson Thanks for the fiddle, I have updated the code so that it can detect the change explicitly, Please follow the updated fiddle jsfiddle.net/7CM6k/3
@TarunGupta yeah it's working on the trigger, but not changing the value. Browsers don't fire the change event when a hidden has its value changed, you have to do it manually.
The first part of this answer about the binding to all hidden fields was very helpful. Thanks!
|
8
$('#userid').change(function(){
  //fire your ajax call  
});

$('#userid').val(10).change();

Comments

5

Building off of Viktar's answer, here's an implementation you can call once on a given hidden input element to ensure that subsequent change events get fired whenever the value of the input element changes:

/**
 * Modifies the provided hidden input so value changes to trigger events.
 *
 * After this method is called, any changes to the 'value' property of the
 * specified input will trigger a 'change' event, just like would happen
 * if the input was a text field.
 *
 * As explained in the following SO post, hidden inputs don't normally
 * trigger on-change events because the 'blur' event is responsible for
 * triggering a change event, and hidden inputs aren't focusable by virtue
 * of being hidden elements:
 * https://stackoverflow.com/a/17695525/4342230
 *
 * @param {HTMLInputElement} inputElement
 *   The DOM element for the hidden input element.
 */
function setupHiddenInputChangeListener(inputElement) {
  const propertyName = 'value';

  const {get: originalGetter, set: originalSetter} =
    findPropertyDescriptor(inputElement, propertyName);

  // We wrap this in a function factory to bind the getter and setter values
  // so later callbacks refer to the correct object, in case we use this
  // method on more than one hidden input element.
  const newPropertyDescriptor = ((_originalGetter, _originalSetter) => {
    return {
      set: function(value) {
        const currentValue = originalGetter.call(inputElement);

        // Delegate the call to the original property setter
        _originalSetter.call(inputElement, value);

        // Only fire change if the value actually changed.
        if (currentValue !== value) {
          inputElement.dispatchEvent(new Event('change'));
        }
      },

      get: function() {
        // Delegate the call to the original property getter
        return _originalGetter.call(inputElement);
      }
    }
  })(originalGetter, originalSetter);

  Object.defineProperty(inputElement, propertyName, newPropertyDescriptor);
};

/**
 * Search the inheritance tree of an object for a property descriptor.
 *
 * The property descriptor defined nearest in the inheritance hierarchy to
 * the class of the given object is returned first.
 *
 * Credit for this approach:
 * https://stackoverflow.com/a/38802602/4342230
 *
 * @param {Object} object
 * @param {String} propertyName
 *   The name of the property for which a descriptor is desired.
 *
 * @returns {PropertyDescriptor, null}
 */
function findPropertyDescriptor(object, propertyName) {
  if (object === null) {
    return null;
  }

  if (object.hasOwnProperty(propertyName)) {
    return Object.getOwnPropertyDescriptor(object, propertyName);
  }
  else {
    const parentClass = Object.getPrototypeOf(object);

    return findPropertyDescriptor(parentClass, propertyName);
  }
}

Call this on document ready like so:

$(document).ready(function() {
  setupHiddenInputChangeListener($('myinput')[0]);
});

3 Comments

In 2020, my previous methods don't work anymore. This approach works the best for me
you made the impossible, POSSIBLE. wish i could upvote you a million times
Very clever! Nice work :)
4

It is possible to use Object.defineProperty() in order to redefine the 'value' property of the input element and do anything during its changing.

Object.defineProperty() allows us to define a getter and setter for a property, thus controlling it.

replaceWithWrapper($("#hid1")[0], "value", function(obj, property, value) { 
  console.log("new value:", value)
});

function replaceWithWrapper(obj, property, callback) {
  Object.defineProperty(obj, property, new function() {
    var _value = obj[property];
    return {
      set: function(value) {
        _value = value;
        callback(obj, property, value)
      },
      get: function() {
        return _value;
      }
    }
  });
}

$("#hid1").val(4);

https://jsfiddle.net/bvvmhvfk/

3 Comments

I like this approach, especially because everyone else's answer is "trigger the change yourself" ... which is not always feasible.
this is almost working for me, everything but the final value in the hidden field isn't actually being changed, if you check the jsfiddle the hidden field value doesn't change from '123' (using Chrome)
Very close, but as the others have mentioned it doesn't maintain a reference to the original mutator/setter for the field, so updates fail to affect the hidden input itself. I'm posting a new solution that incorporates pieces of this approach and the one from stackoverflow.com/a/38802602/4342230
3

I started with Vikars answer, but noticed too that when the hidden form fields used in a HTML form that the last changes didn't get submited. Later on I found Thomas answer, so I combined both to the following solution, which seems to work nicely for my hidden form fields also on submit:

   function replaceWithWrapper(selector, property, callback) {
      function findDescriptor(obj, prop){
         if (obj != null){
            return Object.hasOwnProperty.call(obj, prop)?
               Object.getOwnPropertyDescriptor(obj, prop):
               findDescriptor(Object.getPrototypeOf(obj), prop);
         }
      }

      jQuery(selector).each(function(idx, obj) {
         var {get, set} = findDescriptor(obj, property);

         Object.defineProperty(obj, property, {
            configurable: true,
            enumerable: true,

            get() { //overwrite getter
               var v = get.call(this);  //call the original getter
               //console.log("get '+property+':", v, this);
               return v;
            },
            
            set(v) { //same for setter
               //console.log("set '+property+':", v, this);
               set.call(this, v);
               callback(obj, property, v)
            }
         });
      });
   }

   replaceWithWrapper('#myhiddenfield', 'value', function() {
      console.log('myhiddenfield value changed!');
   });

Comments

1

This example returns the draft field value every time the hidden draft field changes its value (chrome browser):

var h = document.querySelectorAll('input[type="hidden"][name="draft"]')[0];
//or jquery.....
//var h = $('input[type="hidden"][name="draft"]')[0];

observeDOM(h, 'n', function(draftValue){ 
  console.log('dom changed draftValue:'+draftValue);
});


var observeDOM = (function(){
var MutationObserver = window.MutationObserver || 
window.WebKitMutationObserver;

  return function(obj, thistime, callback){
    if(typeof obj === 'undefined'){
      console.log('obj is undefined');
      return;
    }

    if( MutationObserver ){

        // define a new observer
        var obs = new MutationObserver(function(mutations, observer){

            if( mutations[0].addedNodes.length || mutations[0].removedNodes.length ){

               callback('pass other observations back...');

            }else if(mutations[0].attributeName == "value" ){

               // use callback to pass back value of hidden form field                            
               callback( obj.value );

            }

        });

        // have the observer observe obj for changes in children
        // note 'attributes:true' else we can't read the input attribute value
        obs.observe( obj, { childList:true, subtree:true, attributes:true  });

       }
  };
})();

Comments

-5

Although this thread is 3 years old, here is my solution:

$(function ()
{
    keep_fields_uptodate();
});

function keep_fields_uptodate()
{
    // Keep all fields up to date!
    var $inputDate = $("input[type='date']");
    $inputDate.blur(function(event)
    {
        $("input").trigger("change");
    });
}

1 Comment

the blur event is triggered only when the input lose the focus. Is not a valid response for a hidden input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.