Last posts:
Web Design » Web Development » Building a Better Self-Submitting Select
Building a Better Self-Submitting Select
Views: 831 | 1-11-2010, 08:45 | Category : Web Development

This is javascript 101, but I don't see it done too often. A lot of folks like to build select boxes that submit their form when changed. A lot of other folks don't like this, because if a user has javascript turned off, the form won't submit.

A quick and easy way around this is to write a standard self-submitting select, and then to add a submit button to the form. After the submit button's input tag, you can then use a quick line of javascript to hide the button. Voila - if javascript is enabled, the button is hidden, and the select is self-submitting. If javascript is disabled, the button remains, and can be used to submit the form.

Quick example:

<form id="myForm">
<select onchange="document.getElementById('myForm').submit()">
<option>Choose...</option>
<option>Red</option>
<option>Blue</option>
</select>
<input type="submit" value="Go" id="mySubmit" />
<script>
document.getElementById("mySubmit").style.display = "none";
</script>
</form>


Update:

A better solution was presented by Ryan Guill. Use the
<noscript />
tag! Sometimes I wonder about myself...

<form id="myForm">
<select onchange="document.getElementById('myForm').submit()">
<option>Choose...</option>
<option>Red</option>
<option>Blue</option>
</select>
<noscript>
<input type="submit" value="Go" id="mySubmit" />
</noscript>
</form>


Overall point: remember that not everyone uses javascript, and that accounting for it isn't always as hard as it seems.


Tags: Development



Read also:
  • Passing parameter into functions – by value and reference.
  • Ant and Subversion: Building Model-Glue
  • Rails' ActiveRecord, Reactor, Illudium: All backwords for OO.
  • I Don't Like Rails
  • CFCUnit + FlexUnit How-To: No excuses now!

  • Vote
    What do you need?

    Website design
    Website development
    PHP Tutorials
    Other


    Hire Desk
    »
    Website Design & Development, Php Tutorials
    This is Web developer Blog
    Only high quality web developming service