Tuesday, December 10, 2024
Google search engine
HomeLanguagesJavascriptjQuery position() Method

jQuery position() Method

The position() method is an inbuilt method in jQuery that is used to find the position of the first matched element relative to its parent element in the DOM tree.

Syntax:  

$(selector).position()

Parameters: This method does not contain any parameters.

Return Value: This method returns the position of the first matched element.

Example:  In this example, we are using the jQuery position() method.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>The position Method</title>
    <script src=
    </script>
 
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                let x = $("p").position();
                alert("Top position: " + x.top);
            });
        });
    </script>
    <style>
        body {
            display: flex;
            justify-content: center;
        }
 
        div {
            width: 350px;
            height: 100px;
            font-weight: bold;
            padding: 20px;
            font-size: 25px;
            border: 2px solid green;
        }
    </style>
</head>
 
<body>
    <div>
 
        <p>neveropen.</p>
        <!-- Click on this button  -->
        <button>Click Here!</button>
    </div>
</body>
 
</html>


Output: 
jquery-20
Related Articles: 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments