
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to return the id of the first image in a document with JavaScript?
To return the id of the first image, use the images property in JavaScript.
Example
You can try to run the following code to return the id of the first image in a document −
<!DOCTYPE html> <html> <body> <img id="image1" src="https://www.tutorialspoint.com/html5/images/html5-mini-logo.jpg"> <img id="image2" src="https://www.tutorialspoint.com/hive/images/hive-mini-logo.jpg"> <img id="image3" src="https://www.tutorialspoint.com/sas/images/sas-mini-logo.jpg"> <img id="image4" src="https://www.tutorialspoint.com/maven/images/maven-mini-logo.jpg"> <script> var val = document.images.length; document.write("<br>Number of images in the document: "+val); document.write("<br>The id of the first image: "+document.images[0].id); </script> </body> </html>
- Related Articles
- How to return the number of images in a document with JavaScript?
- How to find the name of the first form in a document with JavaScript?
- How to display the title of a document with JavaScript?
- How to use JavaScript to replace the content of a document with JavaScript?
- How to get the number of forms in a document with JavaScript?
- How to show the number of links in a document with JavaScript?
- How to show the full URL of a document with JavaScript?
- How to get the cookies associated with a document in JavaScript?
- How to return the position of a document relative to the collection in MongoDB?
- How to get only the first BOT ID from thes JavaScript array?
- How to get the id of a form containing a dropdown list with JavaScript?
- How to return the color of the text with JavaScript?
- Update a MongoDB document with Student Id and Name
- How to prevent MongoDB from returning the object ID while finding a document?
- Return TRUE if the first string starts with a specific second string JavaScript

Advertisements