0

All, I was stuck with below problem, Please help to review it . I have two input text in the page . Let's say A and B. When loaded the page , A is enabled to input text while B is disabled, After A onblur is fired , I will validate the value of A, if it is valid(equal to string value test), then B is enabled to input text. The problem is the A's on-blur event would be fired when I click anywhere of the page except clicking on the B, Because in that time ,B is disable. The code just looks like below.

<html>
<head>
    <script type="text/javascript" >
        function ATextOnBlur(obj)
        {
            if (obj.value=='test')
            {
                document.getElementById("BTxt").disabled=false;
            }
        }

    </script>
</head>
<body>
    <input type="text" id="ATxt" value="" onblur="ATextOnBlur(this);">
    <input type="text" id="BTxt" value="" disabled="disabled">
</body>
<html>

And I did some test for it ,found even the click event can't be fired for the disabled element.

<html>
<head>
    <script type="text/javascript" >
        function textClick()
        {
            alert('click'); 
        }
    </script>
</head>
<body>
    <input type="text" value="" disabled="disabled" onclick="textClick();">
</body>
<html>

I want to know if there is any solution for it in Jquery ? or it just can't be fixed no matter with JS or Jquery. Thanks.

Joe.wang
  • 11,537
  • 25
  • 103
  • 180

2 Answers2

1

You can check ATxt's value on keyup event instead of blur:

<input type="text" id="ATxt" value="" onkeyup="ATextOnBlur(this);">
<input type="text" id="BTxt" value="" disabled="disabled">
Murat Çorlu
  • 8,207
  • 5
  • 53
  • 78
  • Thanks your reply +1, One problem of you code I can think of is if there exists ajax request in the `ATextOnBlur` function, then it will trigger an ajax request when keyup. I am afraid this would cause extra overload for the server. that means the server would be attacked by someone. thanks. – Joe.wang Sep 26 '13 at 08:18
  • You can make AJAX request by waiting some time after last keyup event. So, you don't make ajax requests for every keyup, only user wrote whole word. – Murat Çorlu Sep 26 '13 at 08:33
1

You should be using onkeypress event. You can refer here - http://www.w3schools.com/jsref/event_onkeypress.asp

You may also want to consider onkeyup and onkeydown as well depending upon your requirement.

vipulsuri
  • 31
  • 2
  • 6