javascript - blur event doesn't work in FireFox - Stack Overflow

I have an asp mvc page. I have a textbox which when a value is entered, and went off of the box should

I have an asp mvc page. I have a textbox which when a value is entered, and went off of the box should execute a javascript function which will roundup the value in the textbox. This works fine in Chrome and IE but not in FireFox.

The following is the code. setAmount() is the javascript function which is supposed to be executed.

<div class="amount-other-input">
    <div class="bold-label" style="margin-right:5px;">£</div>
    <input id="amountOther" type="number" name="other" onblur="setAmount(this)" class="numbersOnly" maxlength="4">
</div>

I tried in adding a timer as suggested in another stackoverflow answer as shown below, but didin't work:

document.getElementById('amountOther').addEventListener("blur", function() {
        var element = this;
        setTimeout(function() {
            setAmount(element);
        }, 1);
    }, true);

I have an asp mvc page. I have a textbox which when a value is entered, and went off of the box should execute a javascript function which will roundup the value in the textbox. This works fine in Chrome and IE but not in FireFox.

The following is the code. setAmount() is the javascript function which is supposed to be executed.

<div class="amount-other-input">
    <div class="bold-label" style="margin-right:5px;">£</div>
    <input id="amountOther" type="number" name="other" onblur="setAmount(this)" class="numbersOnly" maxlength="4">
</div>

I tried in adding a timer as suggested in another stackoverflow answer as shown below, but didin't work:

document.getElementById('amountOther').addEventListener("blur", function() {
        var element = this;
        setTimeout(function() {
            setAmount(element);
        }, 1);
    }, true);
Share Improve this question asked Mar 19, 2015 at 3:36 MasseyMassey 1,1235 gold badges28 silver badges60 bronze badges 2
  • You shouldn't be using onblur in your HTML and addEventListener in your code. Use just the latter, and put your script at the foot of your page. – user1864610 Commented Mar 19, 2015 at 3:40
  • That code does work and I am using firefox – floor Commented Mar 19, 2015 at 3:40
Add a ment  | 

4 Answers 4

Reset to default 2

Finally I have resolved it as follows:

  1. By adding an onchange event
  2. Calling a method to focus for onchange

    function setFocus(sender){ $(sender).focus(); };

As mentioned by @Hobo Sapiens, don't add the blur event on both the html and event listener. Just call the function directly on the onblur event from the html.

Here is a fiddle for it: http://jsfiddle/pyjtL456/2/

Also, what version of firefox are you checking this on?

£ //When using the onblur property with setAmount as the value function setAmount(element) { console.log(element.value); } //When adding the blur event dynamically var amountElement = document.getElementById('amountOther2'); amountElement.addEventListener('blur', function(){ setTimeout(setAmount(this), 1); });

After looking at the problem. I would suggest you adopt this approach. Here is the HTML snippet:

<div class="amount-other-input">
    <div class="bold-label" style="margin-right:5px;">£</div>
    <input id="amountOther" type="number" name="other" onblur="setAmount(this)" class="numbersOnly" maxlength="4" />
    <input id="amountOther2" type="number" name="other" class="numbersOnly" maxlength="4" />
</div>

Javascript Snippet:
<script>

    //When using the onblur property with setAmount as the value
    function setAmount(element) {
        console.log(element.value);
    }

    //When adding the blur event dynamically    
    var amountElement = document.getElementById('amountOther2');
    amountElement.addEventListener('blur', function(){
            setTimeout(setAmount(this), 1);
    });
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744249869a4565116.html

相关推荐

  • javascript - blur event doesn&#39;t work in FireFox - Stack Overflow

    I have an asp mvc page. I have a textbox which when a value is entered, and went off of the box should

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信