I am reading the
jQuery Cookbook at the moment. I will blog some interesting examples that I am come across and pad them out
- if even only slightly - in the hope that they make more sense to the untrained eye.
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 explictly select the "other" radio button even though an "other" indication has already been given?
Now, try this. 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" check box and not want to type anything into the corresponding text field. Good GUI should assume the user will want to behave rationally. 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 with all that in mind, we can include the discussed usability guidelines and come up with 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 code:
Discussion:
- :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 and only one text box will be selected. That's ok.
- The .each(function(){ 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.
- $(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.
- The inputText button and radio button are stored in the local context.
This is for efficiency.
Til the next time take care of yourselves.