Skip to content
Advertisement

Populate next empty text input based on a button click

I have a simple set of buttons that may be clicked in any order. When clicked the button should fill the next available text box.

So far I have only been able to make the button click populate the text box that is in focus. This only really fulfils half of my task.

At the moment I am only looking for vanilla JS solutions rather than JQuery if possible.

JavaScript

Advertisement

Answer

You can add a paramater to your function to update exact text box like this:

JavaScript

and then call it like “addText(‘text’, 3)”

Check this sandbox https://codesandbox.io/s/laughing-einstein-byhf0f?file=/src/index.js:299-472

If by “next available”, you meant a field which doesn’t already have a value then edit your function like this:

JavaScript

For a demo check this sandbox: https://codesandbox.io/s/trusting-browser-lckvy0?file=/index.html

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement