Online Book Reader

Home Category

AJAX In Action [166]

By Root 3952 0
length of its options array to 0. Now we can add the new options to the list. We need to access the XML’s document entry elements, so we call on getElementsByTagName() once again. This time we need to loop through the array of elements it returns, and obtain the text and value pairs from each e. The first child node of each entry is the option text that is to be displayed to the user, and the second child node is the value. Once these two values are obtained, we create a new Option object, passing the option text as the first constructor parameter and the option value as the second. The new option is then added to the target select element, and the process is repeated until all the new options have been added. The method signature for select.add() varies between browsers, so we use a try...catch statement to find one that works. This completes the coding for our double combo box. We can now load up our HTML page, select a region, and see the second drop-down populated directly from the database. Figure 9.7 shows the double-combo list in action. In this example, the Eastern region is selected from the first list, and the corresponding territories are retrieved from the database and displayed in the second list. The Southern region is then selected from the first list, and its corresponding territories fill in the second list.

Figure 9.7 The double-combo list in action

Licensed to jonathan zheng

342

CHAPTER 9

Dynamic double combo

As you can see in figure 9.7, we still have one job left: changing the selection list’s appearance to make it more appealing. The second selection list’s size expands as it is populated with options. We can fix this shift in size by applying a Cascading Style Sheet (CSS) rule to the element.

9.4.2 Applying Cascading Style Sheets

Cascading Style Sheets allow for changes in the visual properties of the selection element. We can change the font color, the font family, the width of the element, and so on. In figure 9.7 we saw that our second select element is initially only a few pixels wide since it contains no options. When the Eastern region is chosen from the first selection list, our second select element expands. This change of size is visually jarring and creates an unpleasant user experience. The way to fix this issue is to set a width for the selection list:

However, there may still be a problem if one of the displayed values is longer than the width we set. In Firefox, when the element is in focus the options under the drop-down list expand to display their entire text. However, in Microsoft Internet Explorer, the text is chopped off and is not visible to the user, as shown in figure 9.8.

To avoid the problem with Internet Explorer, we need to set the width of the selection list to the width of the longest option. Most of the time the only way to determine the number of pixels required to show the content is by trial and error. Figure 9.8 Cross-browser differences in how a select

element is rendered

Licensed to jonathan zheng

Advanced issues

343

Some developers use browser-specific hacks in their CSS only to set the width wider for IE:

style="width:100px;_width:250px"

Internet Explorer recognizes the width with the underscore, while other browsers ignore it. Therefore, IE’s selection box will be 250 pixels wide, while the other browsers’ selection width will be 100 pixels wide. However, it’s inadvisable to rely on browser bugs such as this one, as they may be fixed in a future version of the browser and break the way your page is displayed.

Let’s look now at ways to add more advanced features to our double-combo script.

9.5 Advanced issues

In this chapter, we have built a simplified version of a double-combo script. We send a single parameter to the server, and we return a set of results for the single selected item. You may find that you need to change the way that this application works. You may want to add another element to the form so that you have a triple combo. You

Return Main Page Previous Page Next Page

®Online Book Reader