官术网_书友最值得收藏!

Detecting change

While creating dynamic and interactive websites and web applications, it is useful to know when a user has changed something on the page, such as the value of a selected input, a text input, or any other element that has a modifiable value.

Getting ready

Once more, create a new blank HTML document named recipe-3.html. Ensure that you have the latest version of jQuery downloaded, which can be included into this HTML file.

How to do it…

To learn how to attach change event handlers to various element types, perform the following steps:

  1. Add the following HTML code to the HTML document you have just created, and update the reference to the jQuery library in order to ensure that the latest version of jQuery is being included into the page:
    <!DOCTYPE html>
    <html>
    <head>
        <title>Chapter 2 :: jQuery Events</title>
        <script src="jquery.min.js"></script>
        <script>
    
        </script>
    </head>
    <body>
    <select id="names">
        <option value="Leon">Leon</option>
        <option value="Allyce">Allyce</option>
        <option value="Jane">Jane</option>
    </select>
    <input type="text" value="The large cat sat on the mat" id="cat" />
    </body>
    </html>
  2. Within the script tags, add the following JavaScript code to attach change event handlers on the different elements:
    $(function(){
        $('#names').change(function(){
         var newValue = $(this).val();
         alert("Input value changed to: " + newValue);
        });
        $('#cat').change(function(){
         var newValue = $(this).val();
         alert("Input value changed to: " + newValue);
        });
    });
  3. Ensure that all the changes have been saved. Now, open recipe-3.html in a web browser; if you change the value of one of the elements on the page, you will be presented with an alert informing you of the change.

How it works…

Selecting each input element using $() and then using the .change() function to attach a change event handler allows us to specify the code to be executed once the user has changed the value of each input.

Within the callback function, which is provided to the .change() function as an argument, we can get the new value. Using this, which refers to the selected element, we can use $(this).val() to retrieve the newly chosen value and display it within an alert.

If you open the web page within a browser and change the selected input value to Allyce, an alert will be displayed similar to the one shown in the following screenshot:

How it works…

This is done using .val() to return the value="Allyce" property on the selected option in the drop-down input which has the change event handler attached.

When using the .change() event handler on a text input, this change event will not be fired until the input box has lost focus, that is, the user has clicked on another part of the web page. As it is often desirable to detect an immediate change, you should consider using a key press event to catch these changes instead.

There's more...

The Detecting button clicks recipe discussed the benefits of using the .on() method over using .click(). These benefits also apply in this situation as the .on() method can also be used with the change event. Consider the following code:

$('body').on("change", "#names", function(){
  var newValue = $(this).val();
  alert("Input value changed to: " + newValue);
});

See also

  • Detecting button clicks
  • Detecting key press events on inputs
  • Updating content based on user input
主站蜘蛛池模板: 分宜县| 庆安县| 普兰店市| 兴城市| 弥勒县| 新巴尔虎右旗| 搜索| 大悟县| 新营市| 天台县| 丰都县| 广元市| 山阳县| 平凉市| 卓尼县| 大化| 屯留县| 金寨县| 化州市| 太康县| 正镶白旗| 龙江县| 麦盖提县| 竹山县| 湖口县| 葫芦岛市| 开鲁县| 游戏| 信宜市| 莱州市| 兴城市| 仁化县| 定南县| 和林格尔县| 阳城县| 冕宁县| 余姚市| 涞源县| 大渡口区| 泰宁县| 萨迦县|