1

Only with Javascript

I want to create a form and, depends on your choice, you will see some fields or others.

I thought I can do that with a getElementById() and using innerHTML to override all of the forms, however I'm searching for another way that doesn't override all the form and just add a new input

2
  • 2
    please post what you have written so far Commented Dec 11, 2017 at 14:49
  • I'm waiting for a better solution before I start writing Commented Dec 11, 2017 at 14:52

2 Answers 2

2

Using Javascript, all you need is document.createElement and setAttribute.

var input = document.createElement("input");
input.setAttribute('type', 'text');

Then you can use appendChild to append the created element to the desired parent element.

var parent = document.getElementById("parentDiv");
parent.appendChild(input);
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a simple example using the createElement, using just javascript you can build nice dynamic forms

<!DOCTYPE html>
<html>
<body>

<p>Click the button.</p>

<button onclick="myFunction()">Add Input Field</button>
<button onclick="myFunctionTwo()">Add Radio button</button>


<script>
function myFunction() {
    var x = document.createElement("INPUT");
    x.setAttribute("type", "text");
    x.setAttribute("value", "You Just added a text field ");
    document.body.appendChild(x);
}
function myFunctionTwo() {
    var y = document.createElement("INPUT");
    y.setAttribute("type", "radio");
    document.body.appendChild(y);
}
</script>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.