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

Updating content based on user input

jQuery allows developers to easily process user input and then update the page to reflect this input. The previous recipes of this chapter have looked at detecting changes on input values and clicks on various page elements. This recipe will help you to create a web page that will update a header element based on the title that has been selected from a drop-down menu.

Getting ready

Create a blank HTML document named recipe-4.html, with the latest version of the jQuery library downloaded and ready for use.

How to do it…

Using techniques similar to those you have learned in the previous recipes, perform the following steps to make changes to the DOM based on user interaction:

  1. Add the following HTML code to recipe-4.html, which you have just created; don't forget to update the reference to the jQuery library. This HTML creates a basic HTML web page with a drop-down menu element, allowing the user to choose a number of titles. There is also a header element that we can manipulate with jQuery based on user selection.
    <!DOCTYPE html>
    <html>
    <head>
        <title>Chapter 2 :: jQuery Events</title>
        <script src="jquery.min.js"></script>
        <script>
    
        </script>
    </head>
    <body>
    <select id="title">
    <option value="#">Select your title...</option>
        <option value="1">Title 1</option>
        <option value="2">Title 2</option>
        <option value="3">Title 3</option>
    </select>
    <h1 id="main-title">No Title</h1>
    </body>
    </html>
  2. Add the following JavaScript code within the script tags to attach a change event handler to the select input #title:
    $(function(){
        $('#title').change(function(){
        var titleText = "";
        switch ($(this).val()) {
            case "1":
                   titleText = "This is the text for title 1";
                break;
            case "2":
                titleText = "This is the text for title 2";
                   break;
               case "3":
                   titleText = "This is the text for title 3";
                   break;
           default:
              titleText = "No Title";
         }
            $('#main-title').html(titleText);
        });
    });
  3. Running this web page in a browser and selecting a new option from the drop-down menu will update the text of the header accordingly.

How it works…

First, we instruct jQuery to attach a change event handler to the #title select input using the following code:

$(function() {
  $('#title').change(function(){

  });
}

When the user changes the selected option in the drop-down input, the change event handler is executed.

In the function argument, we can use $(this) to refer to the #title select input and then use $(this).val(); to get its selected value. Once we have this value, we can perform any action we require using JavaScript. In this example, we determine which title has been selected using a JavaScript switch statement, as shown in the following code snippet:

var titleText = "";
switch ($(this).val()) {
    case "1":
      titleText = "This is the text for title 1";
      break;
    case "2":
      titleText = "This is the text for title 2";
      break;
    case "3":
      titleText = "This is the text for title 3";
      break;
    default:
      titleText = "No Title";
}

Depending on the selected title value, we create some text which we then provide to $('#main-title').html();. This will update the #main-title header element's HTML to be the provided text.

This illustrates a very simple task of how a jQuery developer can process user input and perform an action to alter the page.

See also

  • Detecting change
  • Changing page elements on mouse hover
主站蜘蛛池模板: 南康市| 宜良县| 翁源县| 上犹县| 开远市| 内丘县| 车致| 邯郸市| 济南市| 临猗县| 时尚| 重庆市| 色达县| 巧家县| 龙海市| 河津市| 泸溪县| 郑州市| 雅安市| 胶南市| 个旧市| 澄江县| 南岸区| 红原县| 朝阳县| 沽源县| 七台河市| 韶关市| 连云港市| 吉木乃县| 山西省| 邻水| 肥西县| 塔城市| 米泉市| 锡林浩特市| 大宁县| 布尔津县| 阳新县| 沙坪坝区| 建昌县|