views:

321

answers:

3

How do I execute a function in JavaScript when a text box is populated with text? The text box with be hidden from the user. It will be populated by a USB magnetic card swiper.

Pseudo code:

<script language="javascript" type="text/javascript">
    function MyFunction() {
        //execute this function when MyTxtBox is populated
    }
</script>
<asp:TextBox id="MyTxtBox" runat="server" Visible="false" />
A: 

Attach to MyTxtBox's onChange event. You need to do a bit of ASP.NET to produce the appropriate ClientID for use in JavaScript, since ASP.NET will modify the MyTxtBox ID into something else.

<asp:TextBox id="MyTxtBox" runat="server" Visible="false" />
<script language="javascript" type="text/javascript">
    function MyFunction() {
        //execute this function when MyTxtBox is populated
    }

    document.getElementById("<%= MyTxtBox.ClientID %>").onChange = MyFunction;
</script>
David Pfeffer
Will onChange fire if the element stays in focus? If the USB swipe input doesn't automatically advance to the next field and just stays at the end of the string, will browsers fire that onchange event?
Anthony
@Anthony - You are right. The onChange event will not fire unless the focus advances to the next form field. I need to fire off the event once the textbox becomes populated.
Michael Kniskern
+3  A: 

Seems like you're doing this when the page loads. If you are, this would work.

$(document).ready(function(){
   if($('#MyTxtBox').val().length > 0){
      MyFunction();
   }
});

If it's on change:

$(document).ready(function(){
   $('#MyTxtBox').change(function(){
       if($(this).val().length > 0){
          MyFunction();
       }
   });
});
munch
I am getting a "Expected ',' or ')'" compilation error with the on change sample.
Michael Kniskern
Whoops, there should've been a `);` after the end of the change function. It's fixed now
munch
You answer works when the `MyTxtBox` style attribute is not set to `display:none;`. It does not work when the `MyTxtBox` is hidden
Michael Kniskern
See my answer regarding visible=false
Jon P
@Jon P - I am hiding the text box control using CSS and it does not work. I am also trying to set focus during the page load.
Michael Kniskern
@Michael, OK, that is odd. Try "hiding" the text box by positioning it off the view screen, something like position:absolute;right:-1000px;top:-1000px, and not have display:none. See this article for a beter way: http://accessibilitytips.com/2008/03/04/positioning-content-offscreen/
Jon P
@Micahel, Is there a reason it needs to be a TextBox if it's hidden? Could it not also be a hidden input?
munch
@munch, the card readers I'm familiar with work as a virtual keyboard and I think that a hidden field would not accept keyboard input, I could be wrong though.
Jon P
Michael Kniskern
+1  A: 

See munch's answer but use CSS to hide the text box as setting visible = false will result in the text box HTML not being rendered and therefore not being available on the client side.

<style type="text/css">
.USBBox
 {
     position: absolute;
     left: -999em; 
     overflow: hidden;
 } 
 </style>   
<asp.textbox id="MyTextBox" runat="server" CSSClass="USBBox" />

You can then use jQuery's class selector to acces the text box and not worry about name mangling:

%('.USBBox')

If you have a lot of elements on the page however you might be better accessing by id, in which case use the client id to avoid any name mangling issues:

$('#<%= MyTextBox.ClientID %>')

Update

Used CSS solution provided in this link to hide the textbox from the user. Updated the USBBox CSS class with correct solution as setting display:none caused javaScript issues.

Jon P
@Michael, thanks for the updating, glad to have been usefull
Jon P