ASP.NET 2.0 Javascript onblur problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bkarle
    New Member
    • Jul 2007
    • 3

    ASP.NET 2.0 Javascript onblur problem

    I have a text box control and i have coded its "onblur" event t a Javascript function. I want this javascript function to make visible a checkbox based on comaprison between the textbox being blurred and a hidden textbox on the same page. The javascript is not working. Please help. Here is what I have coded in ASP.NET 2.0 page.

    Thanks
    Bhushan (<email snipped>)

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Check Box Check</title>

    <script language="javas cript" type="text/javascript">
    function CheckBoxCheck()
    {
    var t1 = document.getEle mentById("<%=tx t1.ClientId %>");
    var t2 = document.getEle mentById("<%=tx t2.ClientId %>");
    var chk = document.getEle mentById("<%=ch k1.ClientId %>");
    var t1Value = 0;
    var t2Value = 0;

    if ( ( t1.value == "" ) || ( t1.value == null ) || (t1.value == undefined))
    {};
    else
    {
    alert('reached 1');
    if ( ( t2.value == "" ) || ( t2.value == null ) || (t2.value == undefined))
    {};
    else
    {
    t1value = t1.value;
    t2value = t2.value;

    if (t1value == t2value)
    {chk.visible = false;
    alert('reached 3')};
    else
    {chk.visible = true;
    alert('reached 4')};
    };

    };
    }
    </script>

    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt1" runat="server" onblur="CheckBo xCheck()"></asp:TextBox>
    <asp:CheckBox ID="chk1" runat="server" Visible="False" />
    <asp:TextBox ID="txt2" runat="server" Visible="true"> 555</asp:TextBox><br />
    <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
    </form>
    </body>
    </html>
    Last edited by Banfa; Jul 13 '07, 11:51 AM. Reason: Email address removed, please see site FAQ
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, bkarle. Welcome to TSDN!

    What is your code doing that you don't want it to do? Give an example.
    What is your code *not* doing that it is supposed to? Give an example.

    Comment

    • bkarle
      New Member
      • Jul 2007
      • 3

      #3
      The code I pasted does not do what I want it to do.

      What I want it to do is that when I tab away from the textbox (txt1), I want it to automatically compare the values of that textbox with the value in another textbox (txt2) and based on the results of the comparison, make the check box visible (or leave it invisible) .

      Currently, the code does not do it. I left a few alerts in there to give you an idea as to how far my code is working.

      If you copy and paste the code above into a ASP.NET page, it should give you an idea.

      Thanks

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Bhushan.

        Originally posted by bkarle
        Currently, the code does not do it. I left a few alerts in there to give you an idea as to how far my code is working.

        If you copy and paste the code above into a ASP.NET page, it should give you an idea.
        That will be hard for me to set up on my Mac ~_^

        Are you getting any alerts? Is the JavaScript generating an error? Is CheckBoxCheck getting evaluated when the textbox loses focus?

        Comment

        • Suezie
          New Member
          • Jul 2007
          • 1

          #5
          Originally posted by bkarle
          I have a text box control and i have coded its "onblur" event t a Javascript function. I want this javascript function to make visible a checkbox based on comaprison between the textbox being blurred and a hidden textbox on the same page. The javascript is not working. Please help. Here is what I have coded in ASP.NET 2.0 page.

          Thanks
          Bhushan (email removed)

          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head runat="server">
          <title>Check Box Check</title>

          <script language="javas cript" type="text/javascript">
          function CheckBoxCheck()
          {
          var t1 = document.getEle mentById("<%=tx t1.ClientId %>");
          var t2 = document.getEle mentById("<%=tx t2.ClientId %>");
          var chk = document.getEle mentById("<%=ch k1.ClientId %>");
          var t1Value = 0;
          var t2Value = 0;

          if ( ( t1.value == "" ) || ( t1.value == null ) || (t1.value == undefined))
          {};
          else
          {
          alert('reached 1');
          if ( ( t2.value == "" ) || ( t2.value == null ) || (t2.value == undefined))
          {};
          else
          {
          t1value = t1.value;
          t2value = t2.value;

          if (t1value == t2value)
          {chk.visible = false;
          alert('reached 3')};
          else
          {chk.visible = true;
          alert('reached 4')};
          };

          };
          }
          </script>

          </head>
          <body>
          <form id="form1" runat="server">
          <div>
          <asp:TextBox ID="txt1" runat="server" onblur="CheckBo xCheck()"></asp:TextBox>
          <asp:CheckBox ID="chk1" runat="server" Visible="False" />
          <asp:TextBox ID="txt2" runat="server" Visible="true"> 555</asp:TextBox><br />
          <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
          </form>
          </body>
          </html>

          _Bhushan,
          Please try this code. It works fine.
          [Code=html]
          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head id="Head1" runat="server">
          <title>Check Box Check</title>

          <script language="javas cript" type="text/javascript">
          function CheckBoxCheck()
          {
          //var t1 = document.getEle mentById("<%=tx t1.ClientId %>");
          //var t2 = document.getEle mentById("<%=tx t2.ClientId %>");
          //var chk = document.getEle mentById("<%=ch k1.ClientId %>");

          var t1 = document.getEle mentById('<%=tx t1.ClientId %>').value;
          var t2 = document.getEle mentById('<%=tx t2.ClientId %>').value;
          var chk = document.getEle mentById('<%=ch k1.ClientId %>');

          var t1Value = 0;
          var t2Value = 0;

          if ( ( t1 == "" ) || ( t1 == null ) || (t1 == undefined))
          {};
          else
          {
          alert('reached 1');
          if ( ( t2 == "" ) || ( t2 == null ) || (t2 == undefined))
          {};
          else
          {
          t1value = t1;
          t2value = t2;

          if (t1value == t2value)

          {


          document.getEle mentById('divCh eck').style.dis play="none";
          alert('reached 3')};

          else

          {

          document.getEle mentById('divCh eck').style.dis play="block";

          alert('reached 4')};
          };

          };
          }
          </script>

          </head>
          <body>
          <form id="form1" runat="server">
          <div>
          <asp:TextBox ID="txt1" runat="server" onblur="CheckBo xCheck()"></asp:TextBox>
          <div id="divCheck" style="display: none " ><asp:CheckBo x ID="chk1" runat="server" /></div>
          <asp:TextBox ID="txt2" runat="server" Visible="true"> 555</asp:TextBox><br />
          <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
          </div>
          </form>
          </body>
          </html>
          [/CODE]
          Last edited by acoder; Jul 14 '07, 11:59 AM. Reason: code in proper tags

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Suezie. Welcome to TSDN!

            Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

            You will still need to use [/CODE] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

            [CODE=html]
            [CODE=javascript]
            [CODE=php]

            and so on.

            Thanks!

            MODERATOR

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Hi bkarle,

              please note that posting you email address is not allowed on this forum.

              Please read the Posting Guidelines for this and other infomation on what can and can't be posted.

              Regards
              Banfa
              Administrator

              Comment

              • bkarle
                New Member
                • Jul 2007
                • 3

                #8
                Thank you everyone. Suezie's suggestion is great. Basically, I was trying to make the serverside control Checkbox visible/invisible in javascript and it was proving diffcult. It was much easier to have a div (a client side control) and control its visibility (using style attibute such as style="display: none" or style="display: block") using javascript.

                If anyone wants the code, I can post it later.

                Thanks
                Bhushan

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by Banfa
                  Hi bkarle,

                  please note that posting you email address is not allowed on this forum.

                  Please read the Posting Guidelines for this and other infomation on what can and can't be posted.

                  Regards
                  Banfa
                  Administrator
                  Hey, you're back! Where have you been?

                  PS. I removed the email from Suezie's reply too.

                  Comment

                  Working...