• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

Has Object Got Focus - Java Script

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
kinton
Asked:
kinton
  • 3
1 Solution
 
mattfairwCommented:
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
 
kintonAuthor Commented:

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
 
mattfairwCommented:
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
 
mattfairwCommented:
have you run into any problems with this script?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now