In the code that you said you have tried; the function lettersOnly() will never be called and addEventListener() will never be called.
You can simply change it as follow -
<script>
function lettersOnly(input){
var regex = /[^a-z]/gi;
input.value = input.value.replace(regex, "");
}
document.getElementById("username").addEventListener("keyup", lettersOnly(this), false);
</script>
Answer from Tun Zarni Kyaw on Stack OverflowIn the code that you said you have tried; the function lettersOnly() will never be called and addEventListener() will never be called.
You can simply change it as follow -
<script>
function lettersOnly(input){
var regex = /[^a-z]/gi;
input.value = input.value.replace(regex, "");
}
document.getElementById("username").addEventListener("keyup", lettersOnly(this), false);
</script>
Finally, I found myself a solution and I want to share it.
<form>
<label for="username">Choose a Username:</label>
<input type="text" name="username" id="username">
<!--No more “onkeyup” attribute.-->
</form>
<script>
let user = document.getElementById("username");
var regex = /[^a-z]/gi;
user.addEventListener("keyup", ()=>{
user.value = user.value.replace(regex, "");
})
// The “user” variable has been just to simplify a lot of repeated coding, as it is equivalent to “document.getElementById(“username”).
</script>
I don‘t know if this is the best and optimal way to solve my inquire but it works. No “ONkeyup” at all, but using, instead, addEventListener ("keyup", anonymous arrow function). I‘ll really appreciate if anybody has any other suggestion.
Use addEventListener to bind a keyup event to the enter key.
addEventListener ('Keyup') vs HTML attribute 'Onkeyup'
javascript - addEventListener keyup Keydown with Keycode not working - Stack Overflow
trigger a keyup event with javascript?
Videos
I have a browser-side userscript that will fill in a forum input value on an external website that's not mine. It's a search bar. However, there's no 'search' button; the search is fired whenever there's a keyup event in the focused search bar.
The code I'm using so far is this. It properly fills in the input field, but since there's no keyup event, the search function never fires.
document.querySelector(".input_class > input").value = "text";
I feel like there has to be a simple way to fix this. Does anyone have any ideas?
EDIT: Solved! This is what worked for me:
// search for item
let input = document.querySelector(".class > input");
let searchTerm = input.value = valueList;
console.log("Label:", searchTerm);
input.focus();
// simulate keyup event to fire search
let keyUp = new KeyboardEvent('keyup');
input.addEventListener("build", (e) => {
/* … */
});
input.dispatchEvent(keyUp);Do something like this
// Create a named function as your event handler
var myFunction = function (e) {
if (e.keyCode === 13) {
// Do your stuff here
countup();
random();
// Remove event listener so that next time it is not triggered
removeEventListener("keydown", myFunction);
}
};
// Bind "keydown" event
addEventListener("keydown", myFunction);
Idea is user a global variable, set it after firing event.
var is_fired = false;
addEventListener("keydown", function(e){
if (e.keyCode === 13 && is_fired == false) {
countup();
random();
is_fired = true
}
});