Ask Your Question
3

How to retrieve information using JavaScript?

asked 2023-05-24 04:49:12 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-24 05:13:02 +0000

huitzilopochtli gravatar image

There are several ways to retrieve information using JavaScript:

  1. getElementById(): This method is used to retrieve a specific element from the HTML document by its ID attribute.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Get ElementById Example</title>
</head>
<body>
  <h1 id="heading">Hello World!</h1>
  <script>
    var element = document.getElementById("heading");
    alert(element.innerHTML);
  </script>
</body>
</html>
  1. getElementsByClassName(): This method is used to retrieve all the elements that have a specific class name.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Get ElementByClassName Example</title>
</head>
<body>
  <h1 class="heading">Hello World!</h1>
  <p class="desc">This is a description.</p>
  <script>
    var elements = document.getElementsByClassName("heading");
    for(var i = 0; i < elements.length; i++) {
      alert(elements[i].innerHTML);
    }
  </script>
</body>
</html>
  1. getElementsByTagName(): This method is used to retrieve all the elements that have a specific tag name.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript GetElementsByTagName Example</title>
</head>
<body>
  <h1>Hello World!</h1>
  <p>This is a paragraph.</p>
  <script>
    var elements = document.getElementsByTagName("p");
    for(var i = 0; i < elements.length; i++) {
      alert(elements[i].innerHTML);
    }
  </script>
</body>
</html>
  1. querySelector(): This method is used to retrieve the first element in the document that matches a specific CSS selector.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript querySelector Example</title>
</head>
<body>
  <div id="main">
    <h1>Hello World!</h1>
    <p>This is a paragraph.</p>
  </div>
  <script>
    var element = document.querySelector("#main h1");
    alert(element.innerHTML);
  </script>
</body>
</html>
  1. querySelectorAll(): This method is used to retrieve all the elements in the document that match a specific CSS selector.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript querySelectorAll Example</title>
</head>
<body>
  <div id="main">
    <h1>Hello World!</h1>
    <p>This is a paragraph.</p>
  </div>
  <script>
    var elements = document.querySelectorAll("#main p");
    for(var i = 0; i < elements.length; i++) {
      alert(elements[i].innerHTML);
    }
  </script>
</body>
</html>
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-24 04:49:12 +0000

Seen: 11 times

Last updated: May 24 '23