Fetching Elements

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 158:- Fetching Elements

To fetch an element from the DOM using JavaScript, you can use various methods, such as getElementById, getElementsByClassName, getElementsByTagName, querySelector, and querySelectorAll.

Here's a brief explanation of each method:

  1. getElementById(): This method returns the first element that has the ID attribute with the specified value. The method takes a string argument that represents the ID of the element.
  2. getElementsByClassName(): This method returns a collection of elements that have the specified class name. The method takes a string argument that represents the class name.
  3. getElementsByTagName(): This method returns a collection of elements that have the specified tag name. The method takes a string argument that represents the tag name.
  4. querySelector(): This method returns the first element that matches a specified CSS selector. The method takes a string argument that represents the CSS selector.
  5. querySelectorAll(): This method returns a static NodeList containing all elements that match a specified CSS selector. The method takes a string argument that represents the CSS selector.

Here's an example of fetching an element by its ID and changing its text content using JavaScript:

htmlCopy code

<!DOCTYPE html> <html>  <head>    <title>Fetching an Element</title>  </head>  <body>    <h1 id="myHeading">Hello World!</h1>    <script>      // Fetching an element by ID      const heading = document.getElementById('myHeading');      // Changing the text content of the element      heading.textContent = 'Hello JavaScript!';    </script>  </body> </html>

In this example, the getElementById method is used to fetch the element with the ID of myHeading, and the textContent property is used to change the text content of the element.

18. Understanding DOM

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support