Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Has Object Got Focus - Java Script

Posted on 2004-08-10
4
Medium Priority
?
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

636 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