Skip to main content

Command Palette

Search for a command to run...

๐Ÿ“š Mastering jQuery DOM Traversal, Plugins, and Best Practices

Published
โ€ข4 min read
๐Ÿ“š Mastering jQuery DOM Traversal, Plugins, and Best Practices
P
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: ๐Ÿš€ In-depth explorations of emerging technologies ๐Ÿ’ก Practical tutorials and how-to guides ๐Ÿ”งInsights on software development best practices ๐Ÿš€Reviews of the latest tools and frameworks ๐Ÿ’ก Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letโ€™s connect and grow together! ๐ŸŒŸ

๐Ÿ”ท Part 1: Traversing the DOM in jQuery

DOM Traversal means moving through elements โ€” like from parent to child, or finding specific items inside another element. jQuery makes it very easy.


๐Ÿ”น 1. parent()

Use: To find the immediate parent of a selected element.

โœ… Example 1:

htmlCopyEdit<div class="card">
  <p class="info">Click me!</p>
</div>
javascriptCopyEdit$(".info").click(function() {
  $(this).parent().css("border", "2px solid blue");
});

๐Ÿง  Clicking on the <p> will highlight its parent <div>.

โœ… Example 2:

htmlCopyEdit<ul>
  <li><span>Item 1</span></li>
  <li><span>Item 2</span></li>
</ul>
javascriptCopyEdit$("span").hover(function(){
  $(this).parent().css("background-color", "lightyellow");
});

๐Ÿง  Highlights the <li> when the user hovers over the <span>.


๐Ÿ”น 2. children()

Use: To find direct children of an element.

โœ… Example 1:

htmlCopyEdit<div id="menu">
  <button>Home</button>
  <button>About</button>
</div>
javascriptCopyEdit$("#menu").children().css("margin", "10px");

๐Ÿง  Adds margin to all direct child buttons of #menu.

โœ… Example 2:

htmlCopyEdit<div class="box">
  <p>Text 1</p>
  <div><p>Nested Text</p></div>
</div>
javascriptCopyEdit$(".box").children("p").css("color", "red");

๐Ÿง  Only the first <p> will turn red, not the nested one.


๐Ÿ”น 3. siblings()

Use: To select all siblings of an element (elements with the same parent).

โœ… Example 1:

htmlCopyEdit<ul>
  <li>Apple</li>
  <li class="selected">Banana</li>
  <li>Cherry</li>
</ul>
javascriptCopyEdit$(".selected").siblings().css("opacity", "0.5");

๐Ÿง  All items except Banana will become faded.

โœ… Example 2:

htmlCopyEdit<div class="row">
  <span>A</span>
  <span class="active">B</span>
  <span>C</span>
</div>
javascriptCopyEdit$(".active").siblings().hide();

๐Ÿง  Only B will be visible; A and C will be hidden.


๐Ÿ”น 4. find()

Use: To search for descendant elements (children, grandchildren, etc.)

โœ… Example 1:

htmlCopyEdit<div id="section">
  <ul>
    <li>One</li>
    <li>Two</li>
  </ul>
</div>
javascriptCopyEdit$("#section").find("li").css("color", "green");

๐Ÿง  Changes color of all list items inside #section.

โœ… Example 2:

htmlCopyEdit<div class="container">
  <div class="card">
    <p class="title">Product</p>
  </div>
</div>
javascriptCopyEdit$(".container").find(".title").text("Updated Product");

๐Ÿง  Finds the .title text deep inside the .container.


๐Ÿ”ธ Filtering and Chaining Selectors

jQuery allows chaining multiple methods to write clean code.

โœ… Example:

javascriptCopyEdit$(".list").find("li").eq(1).css("font-weight", "bold");

๐Ÿง  Finds the second list item and makes it bold.

โœ… Another Example:

javascriptCopyEdit$("#form").children("input").first().focus();

๐Ÿง  Focuses the first input field inside the form.


๐Ÿ”ท Part 2: jQuery Plugins

๐Ÿ’ก What are Plugins?

A jQuery plugin is a pre-written piece of code that adds extra functionality, like image sliders, date pickers, or modals.


๐Ÿ”น How to Use a jQuery Plugin

  1. Include jQuery

  2. Include Plugin JS/CSS files

  3. Initialize the plugin in JavaScript


โœ… Example: Using a Date Picker

htmlCopyEdit<head>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
</head>
<body>
  <input type="text" id="myDate">
</body>
htmlCopyEdit<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script>
  $(function() {
    $("#myDate").datepicker();
  });
</script>

๐Ÿง  Now the input field shows a calendar popup when clicked.


Plugin TypeUse Case
Owl CarouselResponsive sliders
jQuery UIDatepickers, tooltips, modals
Select2Stylish dropdowns with search
DataTablesInteractive tables

๐Ÿ”ท Part 3: Best Practices for Writing Efficient jQuery

โœ… Tips for Good Performance:

  1. Use caching:

     javascriptCopyEditvar btn = $("#submitBtn");
     btn.click(...);
    
  2. Minimize DOM access: Donโ€™t select the same element multiple times.

  3. Use event delegation:

     javascriptCopyEdit$(document).on("click", ".dynamic-btn", function(){
       alert("Clicked!");
     });
    
  4. Chain methods instead of repeating:

     javascriptCopyEdit$("#box").css("color", "red").slideDown().fadeIn();
    
  5. Avoid document.ready inside loops or events.


๐Ÿ”š Final Real-Life Example: Interactive FAQ Section

โœ… HTML

htmlCopyEdit<div class="container mt-4">
  <h3>FAQs</h3>
  <div class="faq">
    <p class="question">What is jQuery?</p>
    <div class="answer">jQuery is a JavaScript library.</div>
  </div>
  <div class="faq">
    <p class="question">What is AJAX?</p>
    <div class="answer">AJAX allows data to be loaded without page refresh.</div>
  </div>
</div>

โœ… CSS

htmlCopyEdit<style>
  .answer { display: none; color: gray; }
  .question { cursor: pointer; font-weight: bold; }
</style>

โœ… jQuery

javascriptCopyEdit$(document).ready(function() {
  $(".question").click(function() {
    // Collapse all other answers
    $(this).parent().siblings().find(".answer").slideUp();

    // Toggle current answer
    $(this).next(".answer").slideToggle();
  });
});

๐Ÿง  Clicking on a question shows the answer and hides others โ€” using parent(), siblings(), and find() in action!


๐ŸŽฏ Summary

jQuery FeatureReal Use Case
parent()Target a parent container on click
children()Style only direct inner elements
siblings()Show/hide other items in a group
find()Deep search for nested elements
PluginsAdd features like sliders or pickers
Best PracticesKeep your jQuery fast and clean

Let me know if you want article on any other topics as well .

15 views

More from this blog

T

Technologies: Tools & Tips | Expert Guides on Latest Tech Tools

125 posts