Home > Net >  Can I execute javascript when typing on a form?
Can I execute javascript when typing on a form?

Time:10-01

I'm trying to find a way to trigger JavaScript when writing on an input Thanks to everyone can help me

<input id="tophead-searchbar"  placeholder="Cerca" onfocus="mostraanteprimericerca();" onchange="Result1InnerHTML();">

Anyone that can help me?

    function Result1InnerHTML() {
        document.getElementById("Result1").innerHTML = "Cerca"   document.getElementById("tophead-searchbar").innerHTML   "su Nevent";
    }

This is the function I want to call

CodePudding user response:

To get the input as the user types, use the input event. This will also catch content which the user pastes in using the mouse.

Also note that using onX attributes in your HTML is no longer good practice and should be avoided. Use unobtrusive event handlers attached within your JS code. This can be done using the addEventListsner() method.

Finally, input elements don't have any innerHTML, you need to read the value of the control instead.

const input = document.querySelector('#tophead-searchbar');
const output = document.querySelector('#result');

input.addEventListener('input', e => output.innerHTML = `Cerca ${input.value} su Nevent`);
<input id="tophead-searchbar"  placeholder="Cerca" />

<div id="result"></div>

CodePudding user response:

You can oninput event instead of onchange

  • Related