Find the absolute position of an element using jQuery.
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);
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
HTML Code :
JavaScript Code: