- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fetch nth div from an HTML page using jQuery?
To fetch the nth div from an HTML page, use the jQuery eq() method. It is used to access the index of an element in jQuery. The eq() method refers the position of the element.
Example
You can try to run the following code to learn how to fetch nth div from an HTML page using jQuery. Here, we will fetch the 2nd div:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#button1").click(function(){ alert($("div:eq(1)").text()); }); }); </script> </head> <body> <div>Paragraph One</div> <div>Paragraph Two - 2nd div selected</div> <div>Paragraph Three</div> <button id="button1">Which div?</button> </body>
Advertisements