Skip to content
Advertisement

How do I create multiple inputs after a button is clicked (Javascript)

I am new to javascript and I’m trying to make a web app that tests users and also lets them make tests. I’m currently focusing on the making-tests part of the web app. When the user enters the name of the test and the number of questions they want in the test, they click the button and then a javascript function should be called that has a loop that repeats for the number that they entered. In the loop a ‘question’ and an ‘answer’ input should be created. Here is the html for this feature:

JavaScript

And this is the function ‘createNewElement’ in javascript:

JavaScript

From what I’ve read online I think this should work but I must have made some errors in what I’ve written. Any advice on how to do this successfully would be really appreciated.

Advertisement

Answer

You need the Template literals method to append the question(your input fields to the DOM)

Also, you need some element in your HTML, that will have all the multiple input fields.

Example:

JavaScript

The above div will be your container where you can append n -number of fields you want.

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