简体   繁体   中英

Javascript execute onkeyup function

I have the following input in HTML :

<div class="form-group">
    <label for="siretNumber">Numéro de SIRET</label>
    <input onkeyup="checkSIRET(this);" onfocusout="ocr_on_fly(true, this, true)" onfocusin="ocr_on_fly(false, this, true);" type="text" class="form-control" id="siretNumber" value="{{ pdf['siret'] }}">
</div>
<div class="form-group">
     <label for="sirenNumber">Numéro de SIREN</label>
     <input onkeyup="checkSIREN(this)" onfocusout="ocr_on_fly(true, this, true)" onfocusin="ocr_on_fly(false, this, true)" type="text" class="form-control" id="sirenNumber" value="{{ pdf['siret'] }}">
</div>

On the ocr_on_fly function I need to automatically execute the function of the onkeyup attributes. With the following code I get the value of the onkeyup attr but how could I execute it then ?

let attr = input.getAttribute('onkeyup')
// attr = checkSIRET(this); or attr = checkSIREN(this);

Thanks in advance

You could use dispatchEvent to trigger the keyup keyboard event .

input.dispatchEvent(new KeyboardEvent('keyup'));

By triggering the event this way, the this -argument that you pass to your function matches what it would be during a normal keyup event. If needed, you can pass a specific key that should be reported by the event:

input.dispatchEvent(new KeyboardEvent('keyup', {'key':'a'}));

Since you already have the name of the function as a string:

window["functionName"](arguments);

Please refer to this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM