Sunday, 13 November 2016

javascript - Why does change() called only for receiving radio?




I have the following handler setup



form.find("input[name=url_source]").change(function() {
updateUrlSource(jQuery(this))
});


I was thinking it will be attached to all radio buttons with the name. But actually I see in debugger, that only one call occurs and only first condition satisfies below:




function updateUrlSource(source) {
if( source.is(':checked') ) {
// ...
}
else {
// ...
}
}



Is this true? Why?


Answer




But actually I see in debugger, that only one call occurs and only first condition satisfies below:




Correct, change is only fired on the input that received the check, not the others that (may have) lost it. Details buried somewhere in this section of the spec, but I believe this is the critical bit:




If the element is mutable, then: The pre-click activation steps consist of setting the element's checkedness to true. The canceled activation steps consist of setting the element's checkedness to false. The activation behavior is to fire a simple event that bubbles named change at the element.





(My emphasis, and many thanks to Barmar for the assist!)



Here's an example: Live Copy








Radio Behavior



(Look in the console for the output as you click...



















No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...