Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Make That Form User Friendly!

DZone's Guide to

Make That Form User Friendly!

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

I am reading the jQuery Cookbook at the moment and I will blog some interesting examples I am coming across and padding out, if even only slightly. Hopefully this makes more sense to the untrained eye. So let's begin! Consider a form as such:

Who is going to win the Heineken cup next year?

Click on any one of the teams, notice that the radio box is selected. Then start typing in the "Other" text box. Isn't it a bit odd that the team you previously selected is still selected even though you clearly started typing in the "Other" textbox. Does the user seriously have to select the "other" radio button though an "other" indication has already been given? Now, this time clear out the "other" text box and select another team. Then select the "other" radio button. Notice that the "other" textbox does not have focus even though you are hardly going to click the other box and not want to type anything. You are not that irrational. The point is that when the "Other" radio box is selected, it makes sense that its adjacent text box should get focus. Remember, good usability means the user can achieve what they want with less clicking. And remember, good usability means that things are intuitive and GUIs do not display contradictions. So we really want something like this:

Who is going to win the Heineken cup next year?

So how did we do that? Well let's take a look at the code:

<html>
<head>
<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
</head>
<body>
Consider a form as such:
<p>Who is going to win the Heineken cup next year?</P>
<ul id="choice">
<li>
<input name="source" id="source1" type="radio" value="Leinster"/>
<label for="source1">Leinster</label>
</li>
<li>
<input name="source" id="source2" type="radio" value="Munster"/>
<label for="source2">Munster</label>
</li>
<li>
<input name="source" id="source3" type="radio" value="Ulster"/>
<label for="source3">Ulster</label>
</li>
<li>
<input name="source" id="source4" type="radio" value="Connacht"/>
<label for="source4">Connacht</label>
</li>
<li>
<input name="source" id="source5" type="radio" value="Other"/>
<label for="source5">Other</label>
<input name="Source5Txt" id="Source5Txt" type="text"/>
</li>
</li>
</ul>
<script>
$('#choice input:text').(function() {
   // store some variables locally as they are used more than once.
   var $inputText = $(this);
   var $radioBtn = $inputText.siblings('input:radio'); // all sibling radio buttons

   // If any of the sibling radio buttons change, check to see
   $radioBtn.change(function(){
      // if it checked focus on the text input
      if (this.checked) {$inputText.focus();}
   });

   // listen for the blur event on the text input
   $inputText.keypress(function() {
       $radioBtn.attr('checked', true);
   });
});
</script>
</body>
</html>

Discussion:

  1. :text is a JQuery psuedo class Selector which selects all elements of type text. As per JQuery recommendations, pseudo class Selectors should be preceded with another selector or tag element otherwise the universal selector is implied. In this case we do input:text. In this case, only one text box will be selected. That's ok.
  2. The .each(function(){ in this case means iterate overall inputs of type text and execute the specified function. Don't forget ".each" can operate on sets of elements of any size including just 1 which is the case in this example.
  3. $(this) corresponds to the object which is being operated on by each. In this case it is each input text box. If you don't believe me add:
    
      console.log("this is: " + $(this)[0].id);
    
    

    and you will see something like:

    this is: Source5Txt in the console.

  4. The inputText button and radio button are stored in the local context. This is for efficiency.
Until next time, take care of yourselves.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Alex Staveley, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}