Halhelms
SIGN UP FOR MY NEWSLETTER

www.savorgold.com is top on wow gold and runescape gold and World of Warcraft gold provider list for trusted services. Their reputation seems to growing by the minute, which isn't surprising because they are one of the safest sellers of Gold. Delivery speed and customer service are very good. They aslo are giving some bonus items depending on the amount of gold you purchase.

 
 
Halhelms

Recent Comments

Recent Entries

RSS

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="process"> Process<br>
<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.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

Now, let's put this jQuery to use:

<script type="text/javascript">
   $('#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.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Andrew Penhorwood's Gravatar Thanks for the jQuery intro. I was wondering if you might include a working example for reference.
# Posted By Andrew Penhorwood | 3/23/09 10:57 AM
Hal's Gravatar @Andrew Sure

<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>
# Posted By Hal | 3/23/09 11:03 AM
John Quarto-vonTivadar's Gravatar Hal, you should probably make those input boxes xhtml compliant, though, obviously as you know, not for jQuery's sake
# Posted By John Quarto-vonTivadar | 3/23/09 3:04 PM
John Quarto-vonTivadar's Gravatar And also, from the idea of "picking one but not both" then why are these checkboxes rather than radio buttons?
# Posted By John Quarto-vonTivadar | 3/23/09 3:05 PM
Hal's Gravatar @John The reason for the checkboxes is so that you can elect to do neither. If I have a radio button, once one is checked, I can't "uncheck" both (unless I have another called "neither").
# Posted By Hal | 3/23/09 3:18 PM
John Quarto-vonTivadar's Gravatar oooooh, but with jQuery you could make those radio buttons obey the Master from Sarasota, neh?
# Posted By John Quarto-vonTivadar | 3/23/09 3:25 PM
John Quarto-vonTivadar's Gravatar and if not radio buttons then why not a select box? :)
# Posted By John Quarto-vonTivadar | 3/23/09 3:57 PM
Hal's Gravatar @John: With jQuery, I can control the world!...but it wouldn't be nice: users expect radio buttons to behave in their traditional manner.

As for a select box, it's because friends don't let friends use select boxes -- at least not for this sort of thing.
# Posted By Hal | 3/23/09 5:07 PM
Cameron's Gravatar Thanks for starting this series.
I will be looking forward to future entries.
# Posted By Cameron | 3/24/09 4:24 PM
David Mark's Gravatar "So, jQueryDomElement.attr('disabled') returns the value of the "disabled" attribute."

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. :)
# Posted By David Mark | 4/4/09 12:36 AM
Hal's Gravatar So, tell us how you really feel! Seriously, thanks for the feedback on this. I'm a big jQuery fan, but I appreciate a contrary viewpoint. I haven't experienced the sorts of issues you mention with jQuery. You mention the difference between attributes v. properties. Would you mind elucidating?
# Posted By Hal | 4/4/09 9:59 AM
David Mark's Gravatar Hal, this is the last comment as I don't like comments or those ridiculous captcha (sp?) things. Sheesh!

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!
# Posted By David Mark | 4/6/09 6:42 PM
Chris's Gravatar @David: Wow...you must be great at parties.
# Posted By Chris | 4/9/09 2:49 AM
David Mark's Gravatar @Chris

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.
# Posted By David Mark | 4/24/09 7:53 AM
 
   
Clicky Web Analytics