• Home
  • About Mitch
  • Speaking
  • Articles
  • Contact
  • Home
  • About Mitch
  • Speaking
  • Articles
  • Contact

Digital Strategist

WordPress Developer

Content Creator

Unapologetic Punk

Mitch Canter

  • X
  • Bluesky
  • GitHub
  • Twitch
  • YouTube
  • LinkedIn
Tutorial

Under the Hood with WordPress, Part 6: jQuery / JavaScript

CMDR Mitchcraft

Reading time: 3 minutes

This is our final post of a 6-part series on diving “under the hood” with WordPress. Today, we’re covering jQuery and JavaScript – a power language that can manipulate elements and – in some cases – even bring content in, even after the site has loaded.

  • Intro
  • HTML
  • CSS – Introduction
  • CSS – Examples
  • PHP / MySQL
  • jQuery

We’re in the home stretch with our WordPress “Under the Hood” series.  We’ve covered HTML and CSS, the design and structure of your WordPress site.  We discovered PHP, and how it uses the various settings in the MySQL database to display content and set options.  Today, we look at jQuery and JavaScript – code that can change, manipulate, and even load elements depending upon actions or needs the user may take or have.

What is JavaScript?

JavaScript is an “assembly language” – meaning that it’s used to compile and modify information – that can interact with the user or work separately from the page load to modify and enhance content.  Contrary to what people believe, JavaScript has very little relation to Java – it’s closest cousin would actually be “C”.

OK, So What’s jQuery?

jQuery is best described as a JavaScript “library”.  It’s a collection of precompiled scripts and functions that, if written in straight JavaScript, would take multiple lines to write – instead of the very few needed to write in jQuery.  jQuery is actually a focused library dedicated to manipulation of elements, animations, browser events – like clicks, and AJAX – the ability to load content from an outside source based on an event or condition being met.  jQuery is used on nearly 60% of the top 10,000 websites, and is one of the most powerful – and easy to understand – libraries out there.

jQuery Syntax

jQuery works on a “selector” model – you tell the code what to modify and hook it to a function that modifies it.

$(document).ready(function(){
    $("a.click").click(function(){
        $(this).hide();
    });
});

So… to recap:

  1. jQuery checks to see if the document has fully loaded – this ensures there’s no “strangeness” when the code is run – and ensures that any selectors are actually loaded.
  2. A function is hooked onto a selector – in this case, any hyperlink with the class of “click” is a target
  3. This rule defines that once a link is clicked, that link is hidden.  (this) is another name for “self” – and serves as a target for both absolute and relative positioning of elements to target.

Popular jQuery Functions

There are a ton of jQuery functions, but we’re going to look at the ones that you will most likely use working with WordPress.

.addClass()

$("a.click").addClass("added");

Adds  a class to a particular element

.appendTo() / .prependTo()

$( "<p>Test</p>" ).appendTo( ".inner" );

Appends text (to the end) of the specified selectors.  Prepend, on the other hand, adds text to the front of the specified elements.

.click()

$(document).ready(function(){
    $("a.click").click(function(){
        $(this).hide();
    });
});

Runs a function when an element is clicked.

.fadeIn() / .fadeOut() / .fadeToggle()

$(document).ready(function(){
    $("a.click").click(function(){
        $(this).fadeToggle();
    });
});

When this function is activated, it will fade in (or out) the element in question.

.slideUp() / .slideDown() / .slideToggle()

$(document).ready(function(){
    $("a.click").click(function(){
        $(this).slideToggle();
    });
});

Slides an element into- or out-of hiding.  Toggles will allow the element to be triggered on alternating clicks while -Up and -Down will only allow one-direction functionality.

.wrap() / .wrapInner()

$( ".inner" ).wrapInner( "<div class='new'></div>");

Wraps the content in question with the HTML tags.  WrapInner will allow the tags to be on the INSIDE of the elements.

JavaScript, jQuery
  • WordPress 2.7 is Coming To Town!

    WordPress 2.7 is Coming To Town!

    Reading time: 1 minute

    The rumors are WordPress 2.7 will be coming to a download near you sometime tonight.  Release Candidate 1 is widely successful and I’m really looking forward to upgrading to the full version.  If you’re curious as to what the future entails, here are some great posts around the Internet showcasing the new goodness: The Official…

    WordPress
  • Go Update W3 Total Cache, WPTouch, and AddThis RIGHT NOW!

    Go Update W3 Total Cache, WPTouch, and AddThis RIGHT NOW!

    Reading time: 1 minute

    If you’ve updated your plugins in the last 24 hours, go straight back into your website and do it again – there’s a chance you may have downloaded some infected plugins that were hacked into the repository. According to WordPress.org, the plugins AddThis, W3 Total Cache, and WPTouch were infected with a backdoor that lets…

    WordPress