Does jQuery () change work with hidden form elements

I have a map in a popup. I want to transfer the values ​​(coordinates) from this to my main window and run the method after changing the coordinates.

In my main window, I have this event handler

$(".spatial").change ( function () { alert('Handler for .change() called.'); } ); 

For some reason, alert () is not called when this form changes (the value attribute changes).

 <form action="#" id="spatial_points" class="spatial"> <input type="hidden" id="start_point" class="spatial" value=""/> <input type="hidden" id="end_point" class="spatial" value=""/> </form> 

I know that change () only works with form fields. I am wondering if this extends to the hidden form field?

Here is what the above code looks like after choosing a card:

 <form id="spatial_points" class="spatial" action="#"> <input id="start_point" class="spatial" type="hidden" value="(-7.9091601975133266, 127.0170435)"> <input id="end_point" class="spatial" type="hidden" value="(-44.73273833806611, 154.790481)"> </form> 
+4
source share
1 answer

The change event does not fire when a value is changed using JavaScript. And since this is the only way to change the hidden field, such an event will not be generated.

You can, however, start the event handler manually as soon as you change the value:

 $(".spatial").val(something).change(); 
+12
source

All Articles