Javascript DOM HTML Element blur() Method

Introduction

Remove focus from an <a> element:

document.getElementById("myAnchor").blur();

Click the buttons to give focus and/or remove focus from the link.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
a:focus, a:active {
  color: green;
}
</style>/*from ww  w  . j  a  v a  2  s  .c o m*/
</head>
<body>

<a id="myAnchor" href="https://www.java2s.com">Visit java2s.com</a>
<input type="button" onclick="getfocus()" value="Get focus">
<input type="button" onclick="losefocus()" value="Lose focus">

<script>
function getfocus() {
  document.getElementById("myAnchor").focus();
}

function losefocus() {
  document.getElementById("myAnchor").blur();
}
</script>

</body>
</html>

The blur() method can remove focus from an element.

Use the focus() method to give focus to an element.

element.blur();



PreviousNext

Related