• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery on change

jQuery on change

December 3, 2021 Leave a Comment

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:

<style>#div1 {background: green; display: none; color: #fff; margin-top: 20px; } #div2 {background: blue; display: none; color: #fff; margin-top: 20px; } #div3 {background: orange; display: none; margin-top: 20px; }</style><p>Select an option from the dropdown below.</p><form>
  <input class="example" type="text" value="val1">
  <select class="example">
    <option disabled selected value> -- Select an option below -- </option>
    <option value="option1">Click to see contents of div1</option>
    <option value="option2">Click to see contents of div2</option>
    <option value="option3">Click to see contents of div3</option>
  </select>
</form>
<div id="div1">This is div 1</div>
<div id="div2">This is div 2</div>
<div id="div3">This is div 3</div>

<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.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Change Div Text
  • 2.  jQuery focus – Changing Form Styles with the focus() Method
  • 3.  Using jQuery to Remove Class from an HTML Element
  • 4.  Using jQuery to Hide Element by Class
  • 5.  Set the Height of a Div Using jQuery
  • 6.  jQuery width() – Using jQuery to Get the Width of a Div
  • 7.  Using jQuery to Change Label Text
  • 8.  jQuery first() – Get the First Element
  • 9.  Using jQuery to Add a Sibling to an Element
  • 10.  Using jQuery to Select Multiple Ids

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy