0

I appreciate some help if anyone can tell me how we can generate HTML form dynamically based on any JSON object using javascript?

6
  • 1
    This question is too broad to even answer. Definitely need more information than this Commented Jul 14, 2017 at 17:10
  • may i know what more info you require? Commented Jul 14, 2017 at 17:16
  • what will be the contents of the JSON? what goes into the body? which value from JSON goes to which tag? These are the questions to start with Commented Jul 14, 2017 at 17:17
  • the contents will be multiple objects containing data type,data name and data itself. Commented Jul 14, 2017 at 17:24
  • That just explains the contents of any objects but not how it should be mapped to HTML content Commented Jul 14, 2017 at 18:36

2 Answers 2

1

try this seee

You can use an existing library for that.

A simple one is: Angular Dynamic Forms

More standard way is the JSON Schema with this nice implementation Angular Schema Form. See examples on how it works here DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

HTML:

<html>
 <head></head>
 <body>

 <body>
</html>

javascript:

<script>
//create a form
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

//create input element
var i = document.createElement("input");
i.type = "text";
i.name = "user_name";
i.id = "user_name1";

//create a checkbox
var c = document.createElement("input");
c.type = "checkbox";
c.id = "checkbox1";
c.name = "check1";

//create a button
var s = document.createElement("input");
s.type = "submit";
s.value = "Submit";

// add all elements to the form
f.appendChild(i);
f.appendChild(c);
f.appendChild(s);

// add the form inside the body
$("body").append(f);   //using jQuery or
document.getElementsByTagName('body')[0].appendChild(f); //pure javascript

</script>

you can create as many elements as you want dynamically.

1 Comment

why a php file?

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.