Javascript DOM HTML Variable Object get

Introduction

The Variable object represents an HTML <var> element.

We can access a <var> element via document.getElementById():

var x = document.getElementById("myVar");

Click the button to set the color of <var> element to red.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>
<var id='myVar'>test</var>
<script>
function myFunction() {/*  w w  w .j  a  v a2 s.co  m*/
  var x = document.getElementById("myVar");
  x.style.color = "red";
}
</script>

</body>
</html>



PreviousNext

Related