Greytree

TamWiki

For a mouse who is a packrat

Technology » Quick Action On Select
how to quickly do something on a page based on a changing select value

Summary:this is what goes at the top of the site

(redirected from Main.QuickActionOnSelect)

<< Prev: J Query

Up: ^JavaScript^

This is a quick and dirty way of getting your web page to respond to a change in value of a non-form-enclosed <select> tag. This is useful for causing changes to happen based on some specific set of values that you want to offer the user. First I will give the general form, then offer a specific example from some recent work I have done.

General Form

The concept here is that we use the onchange attribute of the <select> tag to trigger a little javascript to do something with the value of the <option> tag that has been selected.

  1. <select name="myselect" id="myselect" onchange="dosomething(this.value)" size="1">
  2.     <option value="option1">option1</option>
  3.     <option value="option2">option2</option>
  4. </select>
  5. <script type="text/javascript" charset="utf-8">
  6. /* <![CDATA[ */
  7.     function dosomething (argument) {
  8.         alert("Option " + argument + " selected");
  9.     }
  10. /* ]]> */
  11. </script>

Before changing the option:

Attach:before.png Δ

Result:

Attach:after.png Δ

An Example

Here's a specific example from some work I've done recently to enable fast theme switching based on applying a parameter to the current location's URL.

  1. <?php
  2.  
  3. $themes = array();
  4. if ($cssdir = opendir('css')) {
  5.     while ($file = readdir($cssdir)) {
  6.         if (preg_match('/^theme_(.*)\.css$/', $file, $matches)) {
  7.             $themes[] = $matches[1];
  8.         }
  9.     }
  10.  
  11.     echo '<p>Select a theme:'.PHP_EOL;
  12.     echo '<select name="themeswitcher" id="themeswitcher" onchange="settheme(this.value)" size="1">'.PHP_EOL;
  13.     foreach ($themes as $themename) {
  14.         echo '<option value="'.$themename.'"'.($themename == $GLOBALS['theme']?' selected ':'').'>'.$themename.'</option>'.PHP_EOL;
  15.     }
  16.     echo '</select>'.PHP_EOL;
  17.     echo '</p>'.PHP_EOL;
  18. ?>
  19.  
  20. <script type="text/javascript" charset="utf-8">
  21. /* <![CDATA[ */
  22.     function settheme   (theme) {
  23.         window.location=location.href + '&theme=' + theme;
  24.     }
  25. /* ]]> */
  26. </script>
  27.  
  28. <?php
  29.  
  30. } else {
  31.     error_log("ERROR: Could not open css directory\n");
  32. }
  33. ?>

In this example, I'm setting up a means for the user to choose a different theme than the one presented. The means to do this are by reloading the page with a query string parameter of theme=newtheme. The various themes are held in the css subdirectory of the application, and have the file naming specification of "theme_" + theme name + ".css". This makes the theme choices easy to pick out amongst the other possible css and other files in the directory.

The css directory is scanned, using the readdir function in php, which returns the files in the directory handle $cssdir one at a time. The file is run through preg_match to weed out the files we don't want, and when a matching file is found the theme name is extracted into the second element of the $matches array (subscript 1). This is appended to an array of theme names for use in setting the options below in the <select> block.

A little label is given to show the user what to do. In this case, I do not use the alternative of setting the first option in the switch block to be some sort of instruction, as I need to have the current theme name selected. After the label, the select block is started. Of note is the onchange attribute, which is set to the javascript function we're going to call whenever the select block changes:

onchange="settheme(this.value)"

will call the settheme function with the currently selected value of the switch block. this.value is a particularly handy way of passing on that currently selected value.

In the foreach loop, we have to check which option corresponds to the currently selected theme, so we can present the user with something to actually change. The currently selected theme is held in the php $GLOBALS array as key theme. The resulting HTML emitted looks like this:

  1. <p>Select a theme:
  2. <select name="themeswitcher" id="themeswitcher" onchange="settheme(this.value)" size="1">
  3.  
  4. <option value="dark" selected >dark</option>
  5. <option value="purple">purple</option>
  6. </select>
  7. </p>
  8.  
  9. <script type="text/javascript" charset="utf-8">
  10. /* <![CDATA[ */
  11.     function settheme   (theme) {
  12.         window.location=location.href + '&theme=' + theme;
  13.     }
  14. /* ]]> */
  15. </script>

As you can see, the currently selected theme is "purple" and the other option is "dark"

Before:

Attach:examplebefore.png Δ

After

Attach:exampleafter.png Δ

You can see that the theme changed!


Tags: Categories: HowTos

Recent Changes | Printable View | Page History | Edit Page
Page last modified on April 17, 2012, at 08:59 PM by ImportText?