DOM (Document Object Model) – Finding Id-TagName-ClassName

Finding HTML elements by id
Finding HTML elements by tag name
Finding HTML elements by class name
Finding HTML elements by HTML object collections

The code:

<!DOCTYPE html>
<html>
<body>

<p id="intro">Hello World!</p>

<script>
// Get the value
x=document.getElementById("intro"); 

// Write the value
document.write("<p>The text from the intro paragraph: " + x.innerHTML + "</p>");
</script>

</body>
</html>

Complete list:

// Finding HTML Elements by Id
var x=document.getElementById("intro");

// Finding HTML Elements by Tag Name
var x=document.getElementsByTagName("p");

// Finding HTML Elements by Class Name
var x=document.getElementsByClassName("intro");
// Finding HTML Elements by HTML Object Collections
<!DOCTYPE html>
<html>
<body>

<form id="frm1" action="form_action.asp">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br>
  <input type="submit" value="Submit">
</form> 


<p>Return the value of each element in the form:</p>
<script>
var x=document.getElementById("frm1");
for (var i=0;i<x.length;i++)
  {
  document.write(x.elements[i].value);
  document.write("<br>");
  }
</script>

</body>
</html>

My official WebSite >