CancelImage Upload

How to focus on or blur a textarea or other form object via JavaScript

To focus on, or blur the curser from a form element in JavaScript is simple. All that you need is the following two functions.

document.getElementById('textareaName').focus();

document.getElementById('textareaName').blur();

As you can see the functions focus() and blur() need to be called upon at the end of the dom objects specified path. In this instance we are using getElementById() to access the textarea. As you can see there is nothing complex about these two functions. Indeed, all that is needed is correct path specifications. Here is another example:

<html>
<head>
<title>How to focus on or blur a textarea or other form object via JavaScript</title>
<script language="javascript">

function textareaFocus()
{

          document.getElementById('textarea1').focus();

}


function textareaBlur()
{

          document.getElementById('textarea1').blur();

}

</script>
</head>
<body>

<textarea id="textarea1"></textarea><br>
<input type="button" onclick="javascript:textareaFocus();" value="Focus">
<input type="button" onclick="javascript:blur();" value="Blur">

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com