jQuery: Find the absolute position of an element using jQuery
jQuery Practical exercise Part - I : Exercise-26
Find the absolute position of an element using jQuery.
Sample Solution:
HTML Code :
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Find the absolute position of an element using jQuery</title>
</head>
<body>
<form id="myForm" action="sample.php">
First name:<br>
<input type="text" id="nam12" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" id="name2" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
JavaScript Code:
var left_position = $("#name2").offset().left - $(document).scrollTop();
console.log('Left: '+left_position);
var top_position = $("#name2").offset().top - $(document).scrollTop();
console.log('Right: '+top_position);
See the Pen jquery-practical-exercise-26 by w3resource (@w3resource) on CodePen.
Contribute your code and comments through Disqus.
Previous: Access form input fields using jQuery.
Next: Convert a jQuery object into a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/jquery-exercises/part1/jquery-practical-exercise-26.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics