Javascript DOM HTML Code Object get

Introduction

The Code object represents an HTML <code> element.

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

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

Click the button to set the color of the code element to red.

View in separate window

<!DOCTYPE html>
<html>
<body>
<code id="myCode">CSS HTML</code>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from   www  .  j a  va  2 s . c o  m
  var x = document.getElementById("myCode");
  x.style.color = "red";
}
</script>

</body>
</html>



PreviousNext

Related