HTML DOM value Property

HTMLWeb DevelopmentFront End Technology

The HTML DOM value property returns a string corresponding to the value of the attribute of an element.

Following is the syntax −

Returning string value

elementAttribute.value

Let us see an example of HTML DOM value property −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM value</title>
<style>
   * {
      padding: 2px;
      margin:5px;
   }
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>HTML-DOM-value</legend>
         <img src="https://www.tutorialspoint.com/jdbc/images/jdbc-mini-logo.jpg">
         <div id="divDisplay">What is src attribute's value?</div>
         <input type="button" value="Get Answer" onclick="getAnswers()">
      </fieldset>
   </form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   function getAnswers() {
      var ans = document.getElementsByTagName('img')[0].attributes[0].value;
      divDisplay.textContent = 'src attribute has a value '+ans;
   }
</script>
</body>
</html>

Output

Before clicking ‘Get Answer’ button −

After clicking ‘Get Answer’ button −

raja
Published on 30-Oct-2019 06:38:17
Advertisements