Len
Len

Reputation: 11

textarea onchange not firing within php script

I have a php page with a form that has a textarea input object. The onchange isn't firing and I can't work out why - it should be so simple! I read through many similar questions online, and I tried adding onkey, onblur, and addEventListener (as per the example below), none of which worked.

But then I discovered that my code works fine in a html page but not in a php page. Is there something about php that makes this event fire differently?

Thanks!

<textarea name="Address" ID="Address" onChange="alert('You just changed the textarea.')">xxx</textarea>
</form>

<script type="text/javascript">
Address.addEventListener('input', () => {
    console.log("You just changed the textarea.");
}, false);

</script>

Upvotes: 1

Views: 171

Answers (3)

Len
Len

Reputation: 11

So, I figured out what was causing the issue. There was a plug-in using the textarea object elsewhere in the app that was effecting any change events.

Many thanks to everyone who took the time to reply. Much appreciated!

Upvotes: 0

Chiu
Chiu

Reputation: 374

Your code have error "Address" on Address.addEventListener(...), because Address is not an object. You need to replace the line:

Address.addEventListener('input', () => {
to
document.getElementById('Address').addEventListener('input', () => {

Upvotes: 2

Ricardo Rocha
Ricardo Rocha

Reputation: 16266

Change the onchange to onkeyup and will work as you expected:

<textarea name="Address" ID="Address" onkeyup="alert('You just changed the textarea.')">xxx</textarea>

Upvotes: 0

Related Questions