Solved

Has Object Got Focus - Java Script

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Uploading files to the web server has become common part of almost any kind of web application. People use different technologies to solve this, but regardless of the technology used, it is always useful to have some kind of progress indicator shown…
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

743 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