Tania Rascia

Skip Navigation
Simple jQuery Examples with Code and Demos

Simple jQuery Examples with Code and Demos

 /  51 responses

Recently, I wrote a beginner’s guide to using and understanding jQuery for Digital Ocean. I found the official jQuery documentation difficult to understand, especially for beginners who only know HTML and CSS. My intent was to make a simple, easy to follow tutorial to introduce the concept of jQuery.

When I first started learning, I would often want to do something simple and common with jQuery, but when I searched I’d find code samples that were very complicated or confusing. Below, I’ve compiled a few examples, with and without CSS styling, for a some common jQuery tasks: accordions, tabs, popups, dropdowns, and navigation that changes when you scroll to an ID. The non-stylized examples will only have the CSS required to make the demo function.

If you don’t know how to use jQuery at all, please read the Introduction to jQuery article first. The embedded demos might not display perfectly on a mobile device, so it’s better to view this post on desktop, or open the demos separately.


An accordion is a list of headers that open and collapse more content when clicked. I chose to make my example have each section stay open when you click, but some versions only have one content panel open at a time. I may have a slight affinity for the word accordion in general…

See the Pen Accordion Styled by Tania (@taniarascia) on CodePen.

Style No Style

// Clicking on the accordion header title...
	$(".accordion").on("click", ".accordion-header", function() {
	// will (slide) toggle the related panel.


Tabs are a form of navigation that switches the content inside a panel. I had a little fun with the style in the demo, but usually they resemble the tabs you might see on a Manila folder.

See the Pen Tabs Styled by Tania (@taniarascia) on CodePen.

Style No Style

// Clicking on the tab...
$(".tab-list").on("click", ".tab", function(e) {
	// will remove all active classes from the tabs...
	// will hide all tab content...
	// and will activate the current tab and content.


A dropdown is a menu that toggles when you click on it. It’s usually indicated by a downward facing arrow.

See the Pen Dropdown Styled by Tania (@taniarascia) on CodePen.

Style No Style

// Clicking away from the dropdown will collapse it.
$("html").click(function() {

// Any nav item that is not an only child...
$("nav ul li a:not(:only-child)").click(function(e) {
	// will be toggled.

	// Opening a new dropdown will collapse any other dropdown.

Popup Modal

A modal is a dialog box that pops up on the screen, usually blocking access to rest of the content until you interact with it in some way. This is often used like an updated alert() function.

See the Pen Modal Styled by Tania (@taniarascia) on CodePen.

Style No Style

// Clicking the open class will open the modal.
$(".open").on("click", function(){
  $(".overlay, .modal").addClass("active");

// Clicking the close class will close it.
$(".close, .overlay").on("click", function(){
   $(".overlay, .modal").removeClass("active");

// Pressing the escape key will also close the modal.
$(document).keyup(function(e) {
  if (e.keyCode === 27) {
    $(".overlay, .modal").removeClass("active");

Change on Scroll

Sometimes you might want to change an element (in this case, the navigation) after scrolling to a certain point. You might want to change th color, or shrink the nav, or make it stick to the top.

See the Pen Scroll Styled by Tania (@taniarascia) on CodePen.

Style No Style

// Activates when you scroll.
$(window).on("scroll", function() {
	var position = $("#begin").offset();

	// If the top of the screen is greater than the #begin element.
	if ($(window).scrollTop() > position.top - 80) { // 80 pixels is the height of my navigation.
		// activate your class.
	} else {
		// otherwise remove it.


Here are two more examples that I was too lazy to stylize.

Smooth Scroll to an ID Simple Image Slider


I hope you found this group of demos fun and helpful. I believe everything can be simplified, and it’s helpful to make visual examples without styles when you’re learning. I might make some more examples in the future if it gets a positive reception.


Hi, my name is Tania Rascia. I've turned down every offer for advertisements or sponsored posts on this website. I write free resources that have helped thousands of people successfully transition into a web development career.

My committment is to no bullshit, no sponsored posts, no ads, and no paywalls. If you enjoy my content, please consider supporting what I do.

Support my work


New articles on web development every two weeks.

Write a response

Your email address will not be published.

All code will be displayed literally.


  • Ramsey says:

    Awesome post. Teaching a high school class how to program. Using your examples as a way to introduce jQuery. So far they enjoy your styling. We learned how to set HTML/CSS but now they are working on JavaScript to make the page more interactive and to produce different elements.