Degradable SELECT onChange

By  on  

Whenever I go to Google Analytics I notice a slight flicker in the dropdown list area. I see a button appear for the shortest amount of time and the poof! Gone. What that tells me is that Google is making their site function whether the user has JavaScript or not. The following is a quick explanation of how it works.

The XHTML

<iframe id="site-frame" src="http://<?php echo $_POST['site'] ? $_POST['site'] : 'scriptandstyle.com'; ?>" style="border:1px solid #ccc;float:right;width:700px;height:500px;"></iframe>

<form action="" method="post">
	<select name="site" id="site">
		<option value="">Select a Site</option>
		<option value="github.com/darkwing">GitHub</option>
		<option value="mootools.net">MooTools</option>
		<option value="scriptandstyle.com">Script & Style</option>
		<option value="twitter.com/davidwalshblog">Twitter</option>
	</select>
	<input type="submit" value="Go" class="button" id="submit-button" />
</form>

My example is a dropdown list that manipulates an iFrame. Note that we use a form tag and submit button so that the site functions well without JavaScript.

The JavaScript

//when the window has loaded...
window.onload = function() {
	//hide the button 
	document.getElementById('submit-button').style.display = 'none';
	//our event function
	var handler = function() {
		if(select.value) {
			document.getElementById('site-frame').src = 'http://' + select.value;
		}
	};
	//add the event listener
	var select = document.getElementById('site');
	if(select.addEventListener){
		select.addEventListener('change',handler,false);
	}
	else {
		select.attachEvent('onchange',handler,false);
	}
};

When the window loads, we hide the submit button and add the event listener. If the user doesn't have JavaScript, all of our JavaScript gets ignored. Simple.

This system is very easy to implement and well worth the effort to make your website more accessible.

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • By
    Create a Photo Stack Effect with Pure CSS Animations or MooTools

    My favorite technological piece of Google Plus is its image upload and display handling.  You can drag the images from your OS right into a browser's DIV element, the images upload right before your eyes, and the albums page displays a sexy photo deck animation...

  • By
    Facebook-Style Modal Box Using MooTools

    In my oh-so-humble opinion, Facebook's Modal box is the best modal box around. It's lightweight, subtle, and very stylish. I've taken Facebook's imagery and CSS and combined it with MooTools' awesome functionality to duplicate the effect. The Imagery Facebook uses a funky sprite for their modal...

Discussion

  1. Even though it is possible to do it this way, I would much rather just wrap the submit button in a noscript tag. Maybe it’s just me, but I think it’s a bit tacky when I see stuff show briefly while loading and then disappear. Are there any issues with using a noscript tag?

  2. Using the “onLoad” event is a bad idea; there’ll be a significant amount of time between the button being visible and it being removed; this flicker isn’t exactly good for the user experience. It’s better to remove it the button as soon as the element exists – you could do this by using one of the psuedo-domReady events present in many of today’s libraries.

  3. @James: My goal was to avoid using a JS lib. I suppose it would be possible to implement a lib-independent DOMREADY function.

  4. Hmmm, given more though, I could simply put the

  5. Might I ask why you are doing this:

    if(select.addEventListener){
    

    Don’t all browsers support it or what?

  6. @Adriaan, only browsers that comply with the W3C event model support addEventListener – IE uses attachEvent. btw, @David, IE doesn’t support event capturing in the same way as other browsers so the third argument ("false") can be removed from select.attachEvent('onchange',handler,false);

  7. why don’t you just move the script bellow the form, this way you are sure you can access the element and don’t have to wait for onLoad

  8. Stephen

    Am I missing something or why do you use getElementById() instead of $()?

  9. @Stephen: because this is framework independent javascript

  10. David, I just want to say that your blog rocks.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!