Skip to content
Advertisement

How to paste multiple values into multiple form inputs in one paste?

Say I copy

JavaScript

And then select an input field, when I paste it, it automatically puts 1 into the first field, 2 into the second field, 3 into the 3rd etc etc.

The values copied will separated by space, comma or a tab.

The form is simple html and I use PHP to insert into database, whatever language the solution is would be ok. I would image its jQUery, even direct me to a relevant jQuery plug in, if it exists.

Advertisement

Answer

I’ve commented the code line-by-line, hopefully that’s enough. I have to head to work and I’ll make any clarifying edits once I get there.

JavaScript
JavaScript

2020 Edit

Since jQuery has fallen largely out of usage, in favor of a framework such as Angular, Vue, or React, I felt that this answer needed a bit of updating.

Below is a 100% Vanilla JS version that will work with the given HTML. Logically it follows the same steps as above, I just renamed some of the variables for readability. If you have any questions just ask.

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