Technology Software

How to Focus Inside the TextArea With JavaScript

    • 1). Create a new HTML file. Insert a form with various input fields, including a text area. For example, type:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml">

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <title>Text Area</title>

      </head>

      <body>

      <form name="commentForm" rows="3" cols="80">

      Name: <input name="name" type="text" /><br/>

      Email: <input name="email" type="text" /><br/>

      Comments:<br/>

      <textarea name="comments" rows="3" cols="50">

    • 2). Create a JavaScript function to bring focus to the text area element. Place the script between the <head> tags in the HTML file. For example, type:

      <script type="text/javascript">

      function focus() {

      document.commentForm.comments.focus();

      }

      </script>

    • 3). Decide when you want to bring focus to the text area element and assign the JavaScript function to the event in the text area element. For example, replace the text area element with

      <textarea name="comments" rows="3" cols="50" onmouseover="focus()">

Leave a reply