Solved

Has Object Got Focus - Java Script

Posted on 2004-08-10
4
434 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

685 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