jQuery -

Finding


What is jQuery Finding?

jQuery provides various methods to find elements in the DOM tree. These methods allow you to search for elements based on different criteria, enabling you to manipulate specific elements efficiently. This tutorial covers the basics and advanced concepts of jQuery finding methods with detailed examples, explanations, and useful tips.


1. Basic Finding

You can use the .find() method to find all descendant elements that match the selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Basic</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css("border", "2px solid red");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
    </div>
    <button id="btn">Find and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to use the .find() method to select all paragraph elements within a div.


2. Finding by ID

You can use the #id selector to find an element by its ID.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Finding by ID</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("#target").css("border", "2px solid blue");
            });
        });
    </script>
</head>
<body>
    <p id="target">This is the target paragraph.</p>
    <button id="btn">Find by ID and Highlight</button>
</body>
</html>
        
    

This example shows how to find an element by its ID and manipulate it.


3. Finding by Class

You can use the .class selector to find elements by their class name.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Finding by Class</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $(".highlight").css("border", "2px solid green");
            });
        });
    </script>
</head>
<body>
    <p class="highlight">This is a highlighted paragraph.</p>
    <p>This is another paragraph.</p>
    <button id="btn">Find by Class and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find elements by their class name and style them.


4. Finding by Tag

You can use the element selector to find elements by their tag name.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Finding by Tag</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").css("border", "2px solid orange");
            });
        });
    </script>
</head>
<body>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <button id="btn">Find by Tag and Highlight</button>
</body>
</html>
        
    

This example shows how to find elements by their tag name and manipulate them.


5. Finding by Attribute

You can use the [attribute] selector to find elements that have a specific attribute.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Finding by Attribute</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p[data-highlight]").css("border", "2px solid pink");
            });
        });
    </script>
</head>
<body>
    <p data-highlight="true">This is a highlighted paragraph.</p>
    <p>This is another paragraph.</p>
    <button id="btn">Find by Attribute and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find elements with a specific attribute and manipulate them.


6. Finding by Multiple Criteria

You can combine multiple selectors to find elements that match all the specified criteria.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Finding by Multiple Criteria</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p.highlight[data-highlight]").css("border", "2px solid brown");
            });
        });
    </script>
</head>
<body>
    <p class="highlight" data-highlight="true">This is a highlighted paragraph.</p>
    <p class="highlight">This is another highlighted paragraph.</p>
    <p>This is another paragraph.</p>
    <button id="btn">Find by Multiple Criteria and Highlight</button>
</body>
</html>
        
    

This example shows how to combine multiple selectors to find and manipulate elements.


7. Finding with Contains

You can use the :contains() selector to find elements that contain a specific text.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery :contains() Selector - Finding with Contains</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p:contains('highlight')").css("border", "2px solid red");
            });
        });
    </script>
</head>
<body>
    <p>This paragraph contains the word highlight.</p>
    <p>This paragraph does not.</p>
    <button id="btn">Find with Contains and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find elements that contain a specific text and style them.


8. Finding Visible Elements

You can use the :visible selector to find all visible elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery :visible Selector - Finding Visible Elements</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div:visible").css("border", "2px solid blue");
            });
        });
    </script>
</head>
<body>
    <div style="display:none;">This is a hidden div.</div>
    <div>This is a visible div.</div>
    <button id="btn">Find Visible Elements and Highlight</button>
</body>
</html>
        
    

This example shows how to find and style all visible elements.


9. Finding Hidden Elements

You can use the :hidden selector to find all hidden elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery :hidden Selector - Finding Hidden Elements</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div:hidden").css("border", "2px solid green");
            });
        });
    </script>
</head>
<body>
    <div style="display:none;">This is a hidden div.</div>
    <div>This is a visible div.</div>
    <button id="btn">Find Hidden Elements and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and manipulate all hidden elements.


10. Finding Form Elements

You can use various form element selectors like :input, :text, :checkbox, etc., to find and manipulate form elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Form Elements - Finding Form Elements</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $(":input").css("border", "2px solid purple");
                $(":text").css("border", "2px solid orange");
            });
        });
    </script>
</head>
<body>
    <form>
        <input type="text" name="name" />
        <input type="checkbox" name="agree" />
        <input type="submit" />
    </form>
    <button id="btn">Find Form Elements and Highlight</button>
</body>
</html>
        
    

This example shows how to find and manipulate different types of form elements.


11. Finding Parent Elements

You can use the .parent() method to find the immediate parent of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .parent() Method - Finding Parent Elements</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").parent().css("border", "2px solid teal");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a child paragraph.</p>
    </div>
    <button id="btn">Find Parent and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and style the parent element of a selected element.


12. Finding Children Elements

You can use the .children() method to find the immediate children of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .children() Method - Finding Children Elements</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").children().css("border", "2px solid pink");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a child paragraph.</p>
    </div>
    <button id="btn">Find Children and Highlight</button>
</body>
</html>
        
    

This example shows how to find and manipulate the children of a selected element.


13. Finding Siblings

You can use the .siblings() method to find all sibling elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .siblings() Method - Finding Siblings</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").siblings().css("border", "2px solid black");
            });
        });
    </script>
</head>
<body>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <button id="btn">Find Siblings and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and style the siblings of a selected element.


14. Finding Next Sibling

You can use the .next() method to find the next sibling of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .next() Method - Finding Next Sibling</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").next().css("border", "2px solid red");
            });
        });
    </script>
</head>
<body>
    <p>This is a paragraph.</p>
    <p>This is the next sibling paragraph.</p>
    <button id="btn">Find Next Sibling and Highlight</button>
</body>
</html>
        
    

This example shows how to find and manipulate the next sibling of a selected element.


15. Finding Previous Sibling

You can use the .prev() method to find the previous sibling of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prev() Method - Finding Previous Sibling</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").prev().css("border", "2px solid blue");
            });
        });
    </script>
</head>
<body>
    <p>This is the previous sibling paragraph.</p>
    <p>This is a paragraph.</p>
    <button id="btn">Find Previous Sibling and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and style the previous sibling of a selected element.


16. Finding All Next Siblings

You can use the .nextAll() method to find all next siblings of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .nextAll() Method - Finding All Next Siblings</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").nextAll().css("border", "2px solid green");
            });
        });
    </script>
</head>
<body>
    <p>This is a paragraph.</p>
    <p>This is the next sibling paragraph.</p>
    <p>This is another next sibling paragraph.</p>
    <button id="btn">Find All Next Siblings and Highlight</button>
</body>
</html>
        
    

This example shows how to find and manipulate all next siblings of a selected element.


17. Finding All Previous Siblings

You can use the .prevAll() method to find all previous siblings of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prevAll() Method - Finding All Previous Siblings</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").prevAll().css("border", "2px solid purple");
            });
        });
    </script>
</head>
<body>
    <p>This is another previous sibling paragraph.</p>
    <p>This is the previous sibling paragraph.</p>
    <p>This is a paragraph.</p>
    <button id="btn">Find All Previous Siblings and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and style all previous siblings of a selected element.


18. Best Practices with Finding

Following best practices when using finding methods ensures efficient and maintainable code.