0

I've create a JavaScript function that fires on the OnKeyPress event of a text box. Basically when the function is called a label is displayed telling the user that caps lock is enabled. As you can see from the method below, if the Caps Lock is on 3 labels are displayed but I'd like to use the passed in parameter to find out which textbox fired the event so that I only display one of the caps lock labels. Any ideas

My JScript function is below with one of the calling textboxs below that:

    function capLock(e) {
        kc = e.keyCode ? e.keyCode : e.which;
        sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
        if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk))
            document.getElementById('divCurrentPasswordCapsLockStatus').style.visibility = 'visible';
            document.getElementById('divNewPasswordCapsLockStatus').style.visibility = 'visible';
            document.getElementById('divConfirmPasswordCapsLockStatus').style.visibility = 'visible';
        else
            document.getElementById('divCurrentPasswordCapsLockStatus').style.visibility = 'hidden';
            document.getElementById('divNewPasswordCapsLockStatus').style.visibility = 'hidden';
            document.getElementById('divConfirmPasswordCapsLockStatus').style.visibility = 'hidden';
    } 

<asp:TextBox ID="txtCurrentPassword" runat="server" TextMode="Password" CssClass="customtxt" onkeypress="capLock(event)"></asp:TextBox>

So I'd like to do something like the following with the JScript Function:

if (e.name=="txtCurrentPassword") 
{
    document.getElementById('divCurrentPasswordCapsLockStatus').style.visibility ='visible';
}
else 
{ 
    document.getElementById('divNewPasswordCapsLockStatus').style.visibility = 'hidden';
    document.getElementById('divConfirmPasswordCapsLockStatus').style.visibility = 'hidden';
}

I'm using ASP.Net 4.0

Thanks

2
  • 1
    can you pass 'this' into your function? e.g. onkeypress="capLock(this, event)" and change your function to be function capLock(sender, e) { Feb 3, 2012 at 12:11
  • Nearly, I've tried this but how do I get the control name from the sender parameter. I've tried the following: if (sender.id == "txtCurrentPassword") { alert("Run my Code"); }. But sender.id returns the full path of the txtCurrentPassword Control. e.g. tabContainerMain_tabMain_txtCurrentPassword } Feb 3, 2012 at 12:31

1 Answer 1

1

Ok so you need to use

ClientIdMode="Static"

See a breakdown of what the modes are http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx

Pretty much this will emit IDs as you specify them in the page, not as the naming container tries and puts them as.

Then can you pass 'this' into your function? e.g.

onkeypress="capLock(this, event)" 

and change your function to be

function capLock(sender, e) {
3
  • Thanks, that works. However all my combos are now drawn differently? The down arrow that is used to display the combo items in no longer there, it just seems to be have replaced with a white box!? Feb 3, 2012 at 14:01
  • :o I'd look at your css as this shouldnt change that. Feb 3, 2012 at 14:07
  • YESSSSSSSSSSSSS!!! Thank you so much. It's all working fine. If you fancy another questions I posted another one earlier which nobody has answered yet. Search: ASP.Net vertical spacing on a ValidationSummary control. Thanks again. Feb 3, 2012 at 14:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.