Javascript Reference - HTML DOM Kbd Object








The Kbd object represents an HTML <kbd> element.

Standard Properties and Events

The Kbd object supports the standard properties and events.

Example

We can access a <kbd> element by using getElementById().


<!DOCTYPE html>
<html>
<body>
<kbd id="myKbd">Keyboard input</kbd>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from   w  w w.j  a va2s.c o m-->
    var x = document.getElementById("myKbd");
    x.style.color = "blue";
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

We can create a <kbd> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--  ww w.  j av a 2  s.  c  o m-->
    var x = document.createElement("KBD");
    var t = document.createTextNode("Keyboard input text");
    x.appendChild(t);
    document.body.appendChild(x);
}
</script>
</body>
</html>

The code above is rendered as follows: