Learning jQuery: Day 1
This post starts a series on learning jQuery. It's meant for beginners to the library. jQuery has received a great deal of attention (deservedly, I assure you) but if you haven't "gotten" jQuery, you may be unsure how -- or why -- you'd use it. This series means to address that.
First, what is jQuery and why should you care about it? jQuery a library of JavaScript code that makes working with JavaScript and the various browsers' document object models (the DOM) easier. If you've been frustrated by how much code it takes to do things in JavaScript and by the different ways browsers implement both the DOM and JavaScript, you'll find that jQuery provides a terrific solution.
jQuery does a lot, but it does it in a wonderfully unobtrusive way. Rather than list all of its features, let's just dig into some code.
Let's take a simple example: you have two checkboxes: one allows the user to assign a workorder to someone else and the other allows the user to process the workorder. Checking on either one will show a short form. But it's important that the user not be able to do both -- that just wouldn't make sense.
Here's the code:
<input type="checkbox" id="reassign"> Reassign
First, let's include the jQuery library. You can download the jQuery framework from jquery.com and include from your server, but perhaps a better plan is to include the code from Google's repository. In addition to the benefit of high availability through Google's extensive server network, your user's browser may already have the library in its cache.
Now, let's put this jQuery to use:
$('#process').click(function(){
toggleCheckbox($('#reassign'));
});
$('#reassign').click(function(){
toggleCheckbox($('#process'));
});
function toggleCheckbox(jQueryDomElement){
if (jQueryDomElement.attr('disabled')){
jQueryDomElement.removeAttr('disabled');
} else {
jQueryDomElement.attr('disabled', 'disabled');
}
}
</script>
The "$" is a reference to the jQuery JavaScript object. It's often used to wrap a DOM element in a jQuery object. So $('#process') gets the DOM element with an id of "process" and wraps it in a jQuery object. (Notice the use of the CSS style selector. jQuery also works with XPath selectors.)
Why bother wrapping it? Because jQuery gives us some terrific helper methods, one of which we can see in our "toggleCheckbox" JavaScript function. In that function, we accept a DOM element wrapped in a jQuery object. Since it's a jQuery object, we can use jQuery methods, one of which is "attr".
The method, "attr", is like many other jQuery methods: when used without an argument it returns a value; when used with an argument, it sets the value.
So, jQueryDomElement.attr('disabled') returns the value of the "disabled" attribute. If a value for that attribute is provided, it sets that attribute's value. So $('#myElement').attr('disabled', 'disabled') disables the DOM element.
All that's left to notice is the "removeAttr" jQuery method that does just what it sounds like.
With a little jQuery, we were able to enable the behavior we needed. In future posts on this topic, we'll see how to use jQuery to do a good deal more client-side programming. In the meantime, if you want to check out the jQuery docs, you can find them at docs.jquery.com.


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/...;
<input type="checkbox" id="process"> Process<br>
<input type="checkbox" id="reassign"> Reassign
<script type="text/javascript">
$('#process').click(function(){
toggleCheckbox($('#reassign'));
});
$('#reassign').click(function(){
toggleCheckbox($('#process'));
});
function toggleCheckbox(jQuerySelector){
if (jQuerySelector.attr('disabled')){
jQuerySelector.removeAttr('disabled');
} else {
jQuerySelector.attr('disabled', 'disabled');
}
}
</script>
As for a select box, it's because friends don't let friends use select boxes -- at least not for this sort of thing.
I will be looking forward to future entries.
Like hell it does. Best to stick to subjects you know.
That goes for the other fifty million bloggers writing about jQuery and its ridiculous, inconsistent and irretrievably broken "attr" method.
This is how you disable an element:
el.disabled = true;
This is how you enable it.
el.disabled = false;
Can't get any simpler. faster or more concise than that. jQuery does nothing but *create* inconsistencies.
And you didn't mention *which* jQuery you are using. All are broken, but in slightly different ways. The 1.3x versions are a last minute attempt to straighten out for IE8. It isn't working as the jQuery developers are only slightly less in the dark about attributes vs. properties.
BTW, 1.2x turned into a pumpkin with the release of IE8. It never worked for more than a few browsers and configurations and always had trouble in IE, but now it is completely unusable (at least if you wish to support IE8!)
http://www.bennadel.com/blog/1007-jQuery-Attr-Func...
http://groups.google.com/group/jquery-en/browse_th...
http://www.mail-archive.com/discuss@jquery.com/msg...
http://www.nabble.com/Bug-in-FAQs-for-checkboxes-t...
http://www.bennadel.com/blog/1007-jQuery-Attr-Func...
One of those mentioned a lot of "painful debugging" to figure out that jQuery can't check checkboxes. (!)
To save you some time:
el.checked = true;
And:
el.checked = false;
So you are betting your life on a function that doesn't help you at all (oh, it supports "chaining", which will make debugging a misery.) On the contrary, it turns getting/setting properties into a nightmare (at least if you need to support more than one browser!)
Learning jQuery is *not* fun, nor is it productive (or even sane.) Learn Javascript and you will see exactly what I mean. :)
You ask me to elucidate on attributes vs. properties? Therein lies the rub. While you have been a "fan" of jQuery, you haven't learned the slightest thing about browser scripting. These are on page one, which is where the jQuery team is stuck.
Good luck!
You can always spot a jQuery maven's knee-jerk response by a preface of "Wow!" (or the presence of the word "snark.") That's your community.
And the party's over for those foolish enough to use this script on the Web (or anywhere else for that matter.)
http://groups.google.com/group/comp.lang.javascrip...#
http://groups.google.com/group/comp.lang.javascrip...#
http://groups.google.com/group/comp.lang.javascrip...#
Lots more where those came from. Coming soon to a bulletin board near you. :)
You're welcome, but even after my tireless intervention over the course of almost two years, the thing is still unusable (except to those who don't know any better.) It is still ten years behind and nowhere near cross-browser. And yes, it still sniffs browsers.
http://www.jibbering.com/faq/faq_notes/not_browser...
Do enjoy the rest of the party. You'll likely be serving food at the next one.