1

May I ask what is the difference between on("change") (jQuery) and .change (JavaScript)?

It will be great if there are snippets/examples for better understanding.

1
  • 3
    Functionally they are the same. Using .on allows you to use event delegation with one of the overloads. Commented Dec 28, 2019 at 9:37

1 Answer 1

1

This is not specific to jQuery Mobile but is a part of the jQuery Framework itself.

See more: https://api.jquery.com/change/

The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

In regards to .change():

This method is a shortcut for .on( "change", handler )

In essence they are the same. Using .on() has more delgation options and therefore is better to use with dynamically created elements that do not exist in DOM when the page loads.

Consider the following HTML:

<div class="form-parts">
  <label for="option-1">Select First Option</label>
  <select id="option-1">
    <option></option>
    <option value="1">Opt 1<option>
    <option value="2">Opt 2<option>
    <option value="3">Opt 3<option>
  </select>
</div>

And consider the following jQuery:

$(function(){
  $("#option-1").change(function(){
    console.log("Selected: " + $(this).val());
  });
});

In this example, the DOM Loads and is aware of the <select> element, so binding the change callback is no issue. Now what if you wanted to prompt the User to make a second choice based on the <option> they selected. If you created that <select> element for this dynamically, there would be no binding to that new element. You can bind the callback when you create the element:

$("<select>", {
  id: "option-2"
})
.insertAfter($("#option-1"))
.change(function(){
  console.log("Selection 2: " + $(this).val());
});

You can also bind a callback using .on() to elements that do not yet exists.

$(function() {
  $(".form-parts").on("change", "select", function() {
    switch ($(this).attr("id")) {
      case "option-1":
        console.log("Selection 1: " + $(this).val());
        if ($(this).val() === "2") {
          var s = $("<select>", {
            id: "option-2"
          }).insertAfter(this);
          $("<label>", {
            for: "option-2"
          }).html("Second Option").insertBefore(s);
          $("<option>").appendTo(s);
          $("<option>", {
            value: "0"
          }).html("No").appendTo(s);
          $("<option>", {
            value: "1"
          }).html("Yes").appendTo(s);
        } else {
          $("#option-2").add($("#option-2").prev("label")).remove();
        }
        break;
      case "option-2":
        console.log("Selection 2: " + $(this).val());
        break;
    }
  });
})
.form-parts label {
  padding: 3px 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-parts">
  <label for="option-1">Select First Option</label>
  <select id="option-1">
    <option></option>
    <option value="1">Opt 1</option>
    <option value="2">Opt 2</option>
    <option value="3">Opt 3</option>
  </select>
</div>

Here you can see that the change event callback is more complex as it is handling the change for all current and potential <select> elements.

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

6 Comments

Thank you so much for the detailed explanation, I have a better understanding now! :)
jQuery 'change' event and plain JavaScript 'change' events - I don't think they are the same, if you add them on radio input, the former will trigger even if checked by a script and the latter will not and wait for click on the radio input itself, not container etc. and also not trigger when deselected because of other radio in form - same selector and everything
@VitaliyTerziev jQuery is a Framework that executes JavaScript. From the jQuery Docs: Bind an event handler to the "change" JavaScript event, or trigger that event on an element.... This method is a shortcut for .on( "change", handler ) in the first two variations, and .trigger( "change" ) in the third. So it should not have any major difference. It is in the end executing JavaScript. See more: api.jquery.com/change
Thanks for your reply, I think I may have seen a very specific case with something else going on beneath the surface.. I tried to replicate in fiddle but was not able to :) will update if I manage to find the diff between the two (if any)
Thanks, that too, it made me investigate this further and it looks the source of my confusion was 'change' event triggered by jQuery (trigger method) after parent is clicked and input is set to checked (with code), and at the same waiting for a change with 'native' event listener, this blog post also helped makandracards.com/makandra/…
|

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.