2

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" />
4
  • What exactly is a TextBox? Is it a textarea? Commented Jan 4, 2010 at 21:05
  • No, it is not a textarea. I can change it if it will help. It is important that is hidden from the user. Commented Jan 4, 2010 at 21:07
  • When you say the value is populated by USB magnetic card swiper, is it done on the server side? if it is then OnChange for the textbox (JS) will probably not fire. So you might have to go with munch's first recommendation. Commented Jan 4, 2010 at 21:23
  • @ps - No, it is done on the client side. The user swipes the card, the page with do a callback and post the card track data on the screen to validate. Commented Jan 4, 2010 at 21:27

3 Answers 3

3

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();
       }
   });
});
Sign up to request clarification or add additional context in comments.

9 Comments

I am getting a "Expected ',' or ')'" compilation error with the on change sample.
Whoops, there should've been a ); after the end of the change function. It's fixed now
You answer works when the MyTxtBox style attribute is not set to display:none;. It does not work when the MyTxtBox is hidden
See my answer regarding visible=false
@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.
|
1

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.

Comments

0

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>

2 Comments

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 - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.