REQUEST A DEMO

jQuery = Simplification

I came across this code today, and it took me a second to figure out what it was doing:

strSortOrder = document.getElementById("ad").options[document.getElementById("ad").selectedIndex].value;

To break it down, it locates a Select control on the page (FindElementById), finds the element again and gets its selected value’s index (selectedIndex), and sets the variable to the value of the option at the specified index (options[index].value).

There is a jQuery plugin that really simplifies this process. Using jquery.selectboxes.js, the code now is as simple as this:

strSortOrder = $("#ad").selectedValues();

This code now does the same thing, but it is also easy to read, and understand. It shortened from 86 characters to 26, and now requires no explanation.

There are many different cases where jQuery can simplify the code.