• 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
  • Post Ordering Made Easy

    Post Ordering Made Easy

    Reading time: 1 minute

    I love using WordPress’ built in custom post types.   I’ve used them for sliders, directories, galleries, and pretty much anything else I can think to use them for.  The one gripe I have, however, is that I can’t manually set the order of the posts.  Just like typical blog posts, I have to arrange…

    WordPress
  • WordPress.com Announces Premium Themes

    WordPress.com Announces Premium Themes

    Reading time: 1 minute

    News from the WordPress.com blog – premium themes are becoming available to WordPress.com subscribers. I am proud to introduce the very first two premium themes on WordPress.com: Headlines and Shelf. Headlines is a sophisticated magazine theme from WooThemes with a Featured Post slideshow, multiple menus, and 15 beautiful color schemes. It’s available now for a…

    WordPress