Archive for the Category jQuery

 
 

How Many Users Have JavaScript Disabled?

An article on the Yahoo developer blog states that about 1% of users to their collection of sites has Javascript disabled.

Makes sense. But what’s particularly interesting is when you express actual numbers:

The second takeaway is that JavaScript-disabled users exist. While 2% of U.S. visitors may not seem like a lot, keep in mind that over 300 million users visit the Yahoo! homepage each month. That means 6 million users visit each month without the benefit of JavaScript. So even though it’s worth spending your time on the JavaScript-enabled version of the site, there are still a non-trivial amount of users out there who won’t be able to use it.

While the percentage of visitors with JavaScript disabled seems like a low number, keep in mind that small percentages of big numbers are also big numbers.

My personal development style is to start with the scaffolding by writing the XHTML without any styling, make sure the site is still readable then add the CSS. After that’s in place, I wave my magic jQuery wand over it and make it do it’s tricks. This usually (USUALLY) ensures that a javascript-less user at least has access to the basics of the site.

But always, always, make sure that a visitor with javascript disabled at least has complete access to your login, contact and homepage. Always.

jQuery for Designers: Manipulating the DOM

Now that you know how to understand the DOM, let’s manipulate it with jQuery! There are a few prerequisites you should do before tackling this tutorial:

How jQuery thinks
My “aha!” moment with jQuery came as soon as I figured out the way jQuery thought. Luckily, this is easy and only two steps:

1. Find some HTML
2. Do something with it

That’s it! That’s literally all you need to know to start understanding and writing your own jQuery. Let’s break it down:

Finding some HTML
In the previous article, we discussed finding things in the DOM. Luckily, jQuery uses the exact same kind of selectors you probably already use in your CSS, like:

  • h1
  • ul#mylist li
  • a:hover

A basic jQuery selector for the same elements look like this:

  • $('h1')
  • $('ul#mylist li')
  • $('a:hover')

The $(' ')part is how you tell jQuery to select that item. Cake, right? Right.

Doing something with it
Now that you’ve selected your element, it’s time to do something with it. In this case, we’re going to add a class of “active” to that item. addClass() is a built-in jQuery method that will add a class to your selection.

To add the “active” class to the same elements from above:

  • $('h1').addClass("active"); find all h1′s in the HTML and do something (add the class) “active” to them.
  • $('ul#mylist li').addClass("active");find every li in your “mylist” unordered list and add the class “active” to them
  • $('a:hover').addClass("active");find every anchor in the hover state in your document and add the class “active” to them

Get it? The basic syntax of jQuery is “select something dot do something semicolon”: $('selectSomething').doSomething();

Some other very handy jQuery methods you’ll likely use a lot:

  • show() and hide() – shows or hides the selection. e.g.: $('h1').hide();
  • removeClass() – remove a class from your selection. e.g.: $('h1').removeClass("active");
  • hasClass() – finds items in your selection with a particular class. e.g.: $('ul#mylist').hasClass("active");
  • fadeIn() and fadeOut() – changes the transparency of your selection slowly, like a prettier show() and hide(). e.g.: $('h1').fadeIn(500);

Did you notice that we added a number to the fadeIn() method? According to the jQuery documentation, fadeIn accepts a duration. One second = 1000, so $('h1').fadeIn(500); brings any h1′s from completely hidden to completely visible in half of a second.

Try this
Download your example docs for this tutorial and open jquery2.html in both your text editor and Firefox. Activate Firebug and go to your console.

We have a list of places here. In the markup, you’ll notice that it’s an unordered list with an id of “places”. In the console of Firebug, type this and click “run”:

$('ul#myplaces').hide();

Bam! List gone! You should know that you’re not removing the list from the DOM, just hiding it in the browser. How would you make it visible again?

Often, you’ll want to edit the styling of an item with jQuery. For example, let’s make the h1 tag red:

$('h1').css("color","red");

The jQuery documentation for css() says we can give css() a property name and then the property, separated by quotes.

One of the most used jQuery methods in UI development is the ability to “select all items”, So, let’s check all of the checkboxes in the “numbers” fieldset:

$('fieldset#numbers input[type="checkbox"]').attr("checked","checked");

We’re finding all of the checkboxes in the “numbers” fieldset, then changing the attributes of the checkbox to checked using jQuery’s attr() method.

As you (hopefully) know, adding checked="checked" in the markup for a checkbox checks that checkbox. jQuery lets us assign attributes to elements in the DOM using syntax you’re already familiar with. Check? Check.

Homework
Using the Firebug console in Firefox:

  • Hide all of the food images on the page. A beverage while you’re learning to code is always helpful, though, so never hide the Newcastle. Hint: you can select multiple items the exact same way you would in your CSS.
  • Make the h1 bold and blue. Feel free to rely on the jQuery documentation for an example.
  • Uncheck all of the checkboxes in the numbers fieldset
  • Check the last checkbox in the letters fieldset

jQuery for Designers: Understanding the DOM

For these tutorials, we’re going to be using a pretty standard HTML document with links, images, lists, paragraphs and divs. Download it here, unzip and open it up in your favorite text editor.

You’ll want to be sure you install the Firebug extension for Firefox. Firebug is an extremely powerful plugin that lets you see, edit and espeically debug your javascript live in the browser. It’s also handy for viewing all aspects of your DOM.

Wait, what’s my DOM?
(Other than the point of this article) The DOM is the Document Object Model. It’s how a browser knows what to do with your HTML code. When you open an HTML file in your browser, the browser reads your code and then starts executing it. Each item in the DOM (links, elements like div tags, lists, images, etc) is an object with attributes.

In this example, the <ul> is an object with a set of attributes including it’s visibility, placement within the browser window and the data that the <ul> tag holds inside of it. In this case, the <ul>:

  • is visible
  • appears after the paragraph, but before the “coolimages” <div>
  • can be referred to with an id of “places”
  • includes data: the 4 <li> objects including their attributes and data

If you were targetting this <ul> in CSS, you would most likely refer to it as ul#places.

The next tutorial will be about selecting your items using jQuery. Using the sample HTML you downloaded above:

  • How would you select the second paragraph?
  • How would you select the unordered list?
  • How would you select the second image?
  • How would you select the third item list?
  • How would you select the “click here” text?