1

In my asp page, i have three text and 2 label box.On text box onblur event i called a javascript function by passing textbox clientid.But i cant get the textbox or label box value from the id i passed.

this is my code...

<script type="text/javascript">

    function num_validate(cid,cid1) {

        var patt = /^\d*(\.\d{1,2})?$/;
        if (cid1 == "t1") {
            var value=this.document.getElementById(cid).value; //failed
            this.document.getElementById(cid).focus(); //failed
            var ff = document.getElementById('<%= l1.ClientID %>').value);//failed
        }
 }

</script>


 <asp:TextBox ID="t1" runat="server" onblur="javascript:num_validate(this,'t1')">   </asp:TextBox>&nbsp;<asp:Label ID="l1" runat="server" Text="3"></asp:Label>

<asp:TextBox ID="t2" runat="server"  onblur="javascript:num_validate(this,'t2')"></asp:TextBox>&nbsp;
        <asp:Label ID="l2" runat="server" Text="5"></asp:Label>

  <asp:TextBox ID="t3" runat="server"  onblur="javascript:num_validate(this,'t3')"></asp:TextBox>

I could not able the text box,label box value by using either cid,cid1...

Please guide me to get out of this issue?

1
  • So basically you are using the control's id's in order to get there value by passing there id's through the function. Correct ? Commented Feb 22, 2013 at 9:56

2 Answers 2

2

this is wrong

var value=this.document.getElementById(cid).value; //failed
this.document.getElementById(cid).focus(); //failed

use instead

var value=cid.value;
cid.focus();
Sign up to request clarification or add additional context in comments.

3 Comments

i100 you are right when you are use "this" why pass second argument there ?
ok i called, without second argument also...that is not a problem
@i100, Couldn't able to get the label box value using var l1 = document.getElementById('<%=l1.ClientID%>').value; what is the problem here...
0

The code below is what I tried and worked:

  <script type="text/javascript">

        function num_validate(element)
        {
            var Cid = element.getAttribute('id');
            var value;
            if(Cid = 't1')
            {
                value = element.value;
                alert(value);
            }

            else if(Cid = 't3')
            {
                value = element.value;
                alert(value);
            }
        }

</script>


   </head>

<body>

    <form id="form1" runat="server">
    <div>

  <asp:TextBox ID="t1" runat="server" onblur="num_validate(this, t1);">
  </asp:TextBox>&nbsp;

  <asp:TextBox ID="t3" runat="server"  onblur="num_validate(this,t3);"></asp:TextBox>

    </div>
    </form>
</body>
</html>

If it didn't work you could have a look at a similar situation @How do I pass a server control's actual client Id to a javascript function?. Hope that helps.

Comments

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.