Solved

Has Object Got Focus - Java Script

Posted on 2004-08-10
4
432 Views
Last Modified: 2008-01-09
Hi,

I am using a conditonal if javascipt statement to populate different elements of a form  if the form elememnt has focus, is the following code determining wether the element has th efocus , it is not failing (not giving an error)but not working either

 function putchar(character){
      
      if (document.form.qtext.focus==true){document.form.qtext.value += character;}

return true;
      }

So I guess my question is, how, in Java Scrpt, do I detect if an object has focus and has anyone got an example?
0
Comment
Question by:kinton
  • 3
4 Comments
 
LVL 3

Expert Comment

by:mattfairw
ID: 11763133
not totally sure what you're trying to accomplish, you may want to post more code..like the where the function gets called. but in your form, you can test for focus using the onFucus event.

<input type="text" name="txtFld" onFocus="this.value += character"> <!--where character is a variable holding a character-->

if this is not what you want, please post more code.

-Matt
0
 
LVL 2

Author Comment

by:kinton
ID: 11763239

im using a virtual keyboard for maths symbols i want to insert any of a numbersymbols in any field with the same instance of the keyboard depending on which field has got focus

the function is

function putchar(character){
      
      if (document.form.qtext.focus==true){document.form.qtext.value += character;}
      
      
      if (document.form.plausible.focus==true){document.form.plausible.value += character;}
      
      if (document.form.correct.focus==true){document.form.plausible.value += character;}

      return true;
      }



the  virtual keyboard part of the page is as follows


<%if subject="2" then%>
      <tr>
      <td> <img src="../images/questiontextmall.gif" width="78" height="6"></td>
      <td colspan="4"><textarea name="qtext" cols="39" rows="3" id="qtext" tabindex="9"></textarea>      </td>
    </tr>
      <tr>
<td width=31%></td>
<td colspan=3 align="center">
<input type="button" onClick=putchar("&frac14;") value="&frac14;" class="key"><input type="button" onClick=putchar("&frac12;") value="&frac12;" class="key"><input type="button" onClick=putchar("&frac34;") value="&frac34;" class="key"><input type="button" onClick=putchar("&sup1;") value="&sup1;" class="key"><input type="button" onClick=putchar("&sup2;") value="&sup2;" class="key"><input type="button" onClick=putchar("&sup3;") value="&sup3;" class="key"><input type="button" onClick=putchar("&times;") value="&times;" class="key"><input type="button" onClick=putchar("&divide;") value="&divide;" class="key"><input type="button" onClick=putchar("&#43;") value="&#43;" class="key"><input type="button" onClick=putchar("&#45;") value="&#45;" class="key"><br><input type="button" onClick=putchar("&#61;") value="&#61;" class="key"><input type="button" onClick=putchar("&#176;") value="&#176;" class="key"><input type="button" onClick=putchar("%") value="%" class="key">

<input type="button" onClick=putchar("&sum;") value="&sum;" class="key"><input type="button" onClick=putchar("&le;") value="&le;" class="key"><input type="button" onClick=putchar("&ge;") value="&ge;" class="key"><input type="button" onClick=putchar("&ne;") value="&ne;" class="key"><input type="button" onClick=putchar("&pi;") value="&pi;" class="key">

<input type="button" onClick=putchar("&radic;") value="&radic;" class="key"><input type="button" onClick=putchar("&infin;") value="&infin;" class="key">

</td>
<td width="3" align="center">&nbsp;</td>
      </tr>
      <% end if %>
0
 
LVL 3

Accepted Solution

by:
mattfairw earned 500 total points
ID: 11763590
alright, i have a dumbed down version of your program. what happens is that when a text area gains focus, it puts that value into a hidden field, when a button is pressed, it checks which field last had focus, then puts the char in that fld.

<html>
<head>
<SCRIPT>
function putchar(character){

     if (document.keyboard.whichFld.value == "qtext")
     {
       alert("txtarea val = " + document.keyboard.qtext.value);
       document.keyboard.qtext.value += character;
     }
}
</SCRIPT>
</head>
<body>

<table><form name="keyboard">
<%subject = 2
  if subject="2" then%>
     <tr>
      <td> <img src="../images/questiontextmall.gif" width="78" height="6"></td>
      <td colspan="4">
        <textarea onFocus="document.keyboard.whichFld.value='qtext'"  name="qtext" cols="39" rows="3" id="qtext" tabindex="9">qtext</textarea>

      </td>
    </tr>
     <tr>
<td width=31%></td>
<td colspan=3 align="center">
<input type="button" onClick=putchar("&frac14;") value="&frac14;" class="key">
<input type="button" onClick=putchar("&frac12;") value="&frac12;" class="key">
<input type="button" onClick=putchar("&frac34;") value="&frac34;" class="key">
<input type="button" onClick=putchar("&sup1;") value="&sup1;" class="key">
<input type="button" onClick=putchar("&sup2;") value="&sup2;" class="key">
<input type="button" onClick=putchar("&sup3;") value="&sup3;" class="key">
<input type="button" onClick=putchar("&times;") value="&times;" class="key">
<input type="button" onClick=putchar("&divide;") value="&divide;" class="key">
<input type="button" onClick=putchar("&#43;") value="&#43;" class="key">
<input type="button" onClick=putchar("&#45;") value="&#45;" class="key"><br>
<input type="button" onClick=putchar("&#61;") value="&#61;" class="key">
<input type="button" onClick=putchar("&#176;") value="&#176;" class="key">
<input type="button" onClick=putchar("%") value="%" class="key">

<input type="button" onClick=putchar("&sum;") value="&sum;" class="key">
<input type="button" onClick=putchar("&le;") value="&le;" class="key">
<input type="button" onClick=putchar("&ge;") value="&ge;" class="key">
<input type="button" onClick=putchar("&ne;") value="&ne;" class="key">
<input type="button" onClick=putchar("&pi;") value="&pi;" class="key">

<input type="button" onClick=putchar("&radic;") value="&radic;" class="key">
<input type="button" onClick=putchar("&infin;") value="&infin;" class="key">
<input type="hidden" name="whichFld">
</td>
<td width="3" align="center">&nbsp;</td>
     </tr>
     <% end if %>
     <a href="javascript:alert(document.keyboard.whichFld.value);">hidden fld value</a>
    </for></table>
</body>
</html>

-Matt
0
 
LVL 3

Expert Comment

by:mattfairw
ID: 11777065
have you run into any problems with this script?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

910 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

23 Experts available now in Live!

Get 1:1 Help Now