Archive by Author

Episode 11: The jQuery Object Explained

The jQuery Object Explained

While the jQuery library can sometimes feel like magic, the man behind the curtains in the jQuery library is the jQuery object. It’s this object that gives the jQuery library its power.

But to understand the jQuery object, first we have to understand a couple of important concepts.

The DOM is the Document Object Model. It’s a representation of an HTML document. What we normally think of as HTML elements become document objects when we’re dealing with JavaScript. A complete document is a collection of document objects.

And when we’re talking about programming, what is an object? An object is a thing – it’s anything. Any thing. Take a dog, for example. That’s an object. Objects have properties that describe them. For example, a dog can be short or tall, can be black or white or brown, can have curly hair or straight hair, can have pointy ears or floppy ones, can be a Great Dane or a Chihuahua or a Dachshund, and on and on. All of these are properties of the object – they describe what the dog is like.

Dogs can also do things – they can run, walk, bark, jump, eat, sleep, swim, fetch, roll over, play dead, pant, and on and on. These are methods of the object. They’re things that the dog can do. Methods are like verbs – they describe what objects can do. Properties are like adjectives – they describe the object.

DOM objects have properties and methods, too. Different DOM objects have different properties and methods, so code we write to deal with one type of DOM object won’t necessarily work for another. In addition, there are differences between different browsers – some of the properties and methods of DOM objects differ depending upon which browser and which version of the browser you’re using.

That all adds up to a tough life for developers who are just trying to get their code working. That’s where jQuery comes in so handy.

You see, when you select items on the page using jQuery’s $ selector, you’re not actually getting a collection of DOM objects. You’re getting back one jQuery object. It’s handy to think of a jQuery object like a box. That jQuery object might be empty if no elements on the page matched the selector. That’s what we discussed last week when we checked to see if the jQuery object was empty. A jQuery object can also contain one or more DOM objects. Now that they’re in their handy box, we can easily deal with them.

The jQuery object has a standard set of properties and methods that work no matter what browser we might be using and that work most of the time, no matter what type of DOM object we might be using. By boxing up all the DOM objects into a standard box, jQuery helps to smooth out differences from object to object and from browser to browser.

In addition, jQuery also provides lots of handy methods for performing the sorts of tasks JavaScript developers need to do all the time – changing the values of form elements, updating the text or CSS classes of elements, changing CSS properties, hiding elements, showing them again, animating them, etc.

The jQuery object is what makes the jQuery library seem so magical – I hope you now better understand the jQuery object and a bit more about how the jQuery library works. Happy coding!

Episode 10: If an Element Exists

How to tell if an element exists

When you’re working with jQuery, sometimes you need to figure out if an element that you’re trying to select actually exists on the page. You’d think this would be simple and straightforward, but there’s actually a little trick to it that I’ll share with you in this video.

We get started with a list of animals – one of them has a class of highlighted and elsewhere on the page, we want to create a little callout to put the highlighted animal in the spotlight.

We get started by adding a new element to the page using skills that we learned in Episode 3. Then we just add a few quick styles to the new element in CSS just so that we can see it on the page.

Next, we learn how to use jQuery text() method, both to add text to our new element and to fetch the text from another element. This works great as long as there’s an element on the page with the class of highlighted, but if it doesn’t exist, the page looks odd. We want to set up some different text if the highlighted class is missing from the page.

We talk just a bit about JavaScript and how to test if things are true. Many values in JavaScript aren’t actually strictly true or false, but rather truthy or falsey – in a sort of gray area. If a variable is empty and I test it, it returns falsey. Which means that if we test the variable where we used our selector, it should return falsey since it’s empty, right?

Well, not with jQuery. Because if a variable holds a jQuery selector, that variable is never actually empty because it holds a jQuery object. The jQuery object might be empty, but the variable doesn’t know that. It just knows it’s got a jQuery object so it returns a truthy value.

Huh?

Think of a jQuery object like a box. A box isn’t nothing – it’s definitely something. If you’re holding a box, then you’ve definitely got something in your hands. Now, that box might be empty or it might have a kitten or a pie in it. Either way, you’ve got something in your hands.

That’s how a jQuery object works. The jQuery object is something, even when it’s empty. So to test if an element that we’re trying to select is actually on the page, we have to test if the jQuery object is empty or not, not the variable where we used our selector.

We can test that by using the length property of the jQuery object to see if there’s anything in the box. If the length is greater than 0, then the box contains something. Otherwise, the box is empty.

Episode 9: Animated Tabs

Animated Tabs

In Episode 4, we learned how to create simple tabs without using a jQuery plugin. In today’s episode, we’ll make those tabs even spiffier by adding a cool animated transition when switching tabs.

We’ll get started with the code we created in Episode 4, which you can download from GitHub.

The HTML will remain exactly the same. In the CSS, we just have to make one small change – by moving styling from the individual <div>s with a class of .tab and instead applying those styles to the <div> that wraps the collection of tab content <divs>s.

The rest of the changes happen in our JavaScript. And even most of that stays the same. We just remove the single line of code that handled the hiding and showing of the different sections of tab content, then write a new couple lines of code to add the animation.

We’re actually combining three animations. When we click a new tab, first we fade out whatever content happens to be visible at the time. Next, we animate the height of the container to match the height of the new tab content section. Finally, we fade in the content of the new tab container.

Combined, these three animations provide a smooth transition between the content of the different tabs. It’s a little extra something something that helps our websites feel more polished and professional. Thanks to jQuery adding this kind of extra bit of polish is simple and straightforward.

In this video, we take our first look at jQuery’s animate() method, and we also use a callback function for the first time. We’ll dig more into animation and callbacks in future episodes, so stay tuned!

Episode 8: Parents and Children

Parents and Children

While the title of this week’s video might sound like some sort of family therapy session, we’re actually going to be digging into a few of our methods for traversing the DOM – moving up the DOM with parent() or parents() and then moving down the DOM with children() or find().

There’s a lot of confusion and misunderstanding around these methods, so this week’s episode helps to clear that up. We start out with a document with lots of nested divs, each with a different background color just to make them easy to see.

First up, we take a look at parent() and parents(). The parent() method selects only one element – the direct parent element of the element we started from. The parents() method, on the other hand, will select all parent elements, including the <body> and the <html> elements.

But, with either the parent() or parents() methods, we’re able to add a selector and refine the selection that gets made. With the parent() method, that would select the parent element only in the event that it matches the selector. With the parents() method, you can filter out the elements that get selected – rather than selecting every single parent element all the way up to the <html> element, we can say exactly which elements should get selected.

Now that we understand parent() and parents(), let’s take a look at the differences between children() and find(). First up, the find() method requires a selector. If we need to select all the children elements nested inside the element we started from, then we can pass the universal selector (*) to the find() method, like this: find('*')

The children() method, on the other hand gets just the direct children. That throws a lot of people off, since it seems backwards. If we have parent() and parents(), shouldn’t we also have child() and children()?

But working our way up the DOM isn’t the same as working our way down. When we’re searching through ancestors, each element can have only one direct parent element. When we’re searching through descendants, each element could have 0, 1, or many direct children, plus additional descendants nested inside those children.

Just like with parents(), we can also pass a selector to children() to select only those direct children that match the selector rather than all of the direct children.

After working with our many nested divs page, we’ll take a look at a more real-world example – we have a block of content inside a div, with one paragraph that happens to be wrapped in a blockquote. Using our children() and find() methods, we see the difference between them quite clearly. Using the children() method, the paragraph inside the blockquote isn’t selected. But when we switch over to the find() method, we’re able to select that paragraph along with the others.

That’s a pretty good look into parent(), parents(), find(), and children(). Have fun experimenting with those methods to make sure you really understand the differences and next week we’ll take a look at adding animation to Simple Tabs example to make those tabs extra special.