How to find the value of a button with JavaScript?

JavascriptWeb DevelopmentFront End Scripts

Use the value property to get the value of a button in JavaScript.

Example

You can try to run the following code to find the value of a button −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <form id="myForm">
         <button id="btn" type="button" value="my_button">My Button</button>
      </form>
      <script>
         var str1 = document.getElementById("btn").innerHTML;
         var str2 = document.getElementById("btn").value;

         document.write("Button text: "+str1);
         document.write("<br>Button value: "+str2);
      </script>
   </body>
</html>
raja
Published on 26-Feb-2018 12:59:54
Advertisements