Search code examples
javascriptjqueryjquery-eventsdispatchevent

Uncaught InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is null


I am getting weird behavior where Javascript event that I am trying to dispatch is not being interpreted as event while when I inspect it is is Event.

enter image description here

Then it fails with following error:

Uncaught InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is null.

JSFiddle

I must be missing something obvious here.

$(function () {
    $("[type='tel']").keydown(function (event) {
        var position = this.selectionStart;
        var $this = $(this);
        var val = $this.val();
        if (position == this.selectionEnd &&
            ((event.keyCode == 8 && val.charAt(position - 1) == "," && val.substr(0, position - 1).indexOf(".") == -1)
            || (event.keyCode == 46 && val.charAt(position) == "," && val.substr(0, position).indexOf(".") == -1))) {
            event.preventDefault();
            if (event.keyCode == 8) {
                $this.val(val.substr(0, position - 2) + val.substr(position));
                position = position - 2;
            } else {
                $this.val(val.substr(0, position) + val.substr(position + 2));
            }
            $this.trigger('keyup', { position: position });
        } else {
            if (event) {
                this.dispatchEvent(event);
            }
        }
    });

    $("[type='tel']").keyup(function (event, args) {
        if (event.which >= 37 && event.which <= 40) {
            event.preventDefault();
        }

        var position = args ? args.position : this.selectionStart;
        var $this = $(this);
        var val = $this.val();
        var parts = val.split(".");
        var valOverDecimalPart = parts[0];
        var commaCountBefore = valOverDecimalPart.match(/,/g) ? valOverDecimalPart.match(/,/g).length : 0;
        var num = valOverDecimalPart.replace(/[^0-9]/g, '');
        var result = parts.length == 1 ? num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") : num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") + "." + parts[1].replace(/[^0-9.]/g, "");
        $this.val(result);
        var commaCountAfter = $this.val().match(/,/g) ? $this.val().match(/,/g).length : 0;
        position = position + (commaCountAfter - commaCountBefore);
        this.setSelectionRange(position, position);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input class="tooltip form-control input-validation-error" data-val="true" data-val-number="The field Sale price (£) must be a number." data-val-range="The sale price must be between £10,000 and £50,000,000" data-val-range-max="50000000" data-val-range-min="10000" data-val-regex="Sale price must be numeric characters only" data-val-regex-pattern="^\d+$" data-val-required="Please enter a sale price" id="SalePrice" name="SalePrice" placeholder="" tabindex="7" type="tel" value="">


Solution

  • I just had this problem, and it was solved by cloning the event:

    if (event && event.originalEvent) {
        var oe = event.originalEvent;
        this.dispatchEvent(new oe.constructor(oe.type, oe));
    }