When a user is interacting with your website or app, one of the best ways to change elements on the screen based on certain click actions they make is by using the jQuery change() method.

$("#div").change(function() {
  //change something based on what the user has selected or clicked
});

Another way this can be done is to use the .on() method:

$('#div').on("change", function() {
    //change something based on what the user has selected or clicked
});

Below is another example of using the change() method. We will show the example, and then show the full code for it below it. THis example also uses the jQuery show() and hide() methods.

Code Output:

Select an option from the dropdown below.


This is div 1
This is div 2
This is div 3

Full Code:

Select an option from the dropdown below.

This is div 1
This is div 2
This is div 3
<script> $("select").change(function(){ if($(this).find("option:selected").attr("value") == "option1"){ $("#div1").show(); $("#div2").hide(); $("#div3").hide(); } else if($(this).find("option:selected").attr("value") == "option2"){ $("#div1").hide(); $("#div2").show(); $("#div3").hide(); } else if($(this).find("option:selected").attr("value") == "option3"){ $("#div1").hide(); $("#div2").hide(); $("#div3").show(); } }); </script>

Hopefully this article has been useful in helping you to understand how to use the jQuery change() method.

Categorized in:

jQuery,

Last Update: February 26, 2024