Solved

javascript - set focus on particular control

Posted on 2011-02-18
4
458 Views
Last Modified: 2012-05-11
I have 5 textarea...and I need to control which one has focue...how is that done?

    <TEXTAREA onKeyUp="javascript:countChars();" id=textarea1 name=textarea1 rows=7 cols=43></TEXTAREA><br />
      No. of characters:&nbsp; <INPUT id=text1 name=text1 style="borderWidth:0;border:none;" size=5 value=0 readonly>
      <BR> <BR>
0
Comment
Question by:GlobaLevel
  • 3
4 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
Comment Utility
document.getElementById("textarea1").focus();
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
"javascript:countChars(this);"

and add a param to the js function

function countChars(ta){
... use ta, it will give you which textarea you are working with...
}
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
if you want to know which one has focus, then add the following javascript at body onload event

var latestTextArea;
document.getElementsByTagName("textarea").onkeyup = function(this){
  latestTextArea = this.getAttribute("id");
};

0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
now when ever you will press a key, 'latestTextArea' will be updated with the id of the latest TextArea

you can also do the same on the onfocus event (just replace onkeyup with onfocus)

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now