0

My app uses javascript to generate all the HTML for my website. The index.html basically just contains

<div id="menu"></div><div id="content/><div id="content2/>

And the javascript replaces the divs. I find myself doing a lot of repetitive tasks when I need to create a form.

var html = "<form id="form1"> ... </form>";

Is there a good framework to generate HTML forms so that you could do something like

generateForm(<formId>, [{name: "field1", type: button}, {name: "field2", type: input}]);

Or something similar?

2
  • 1
    So when I disable javascript I get a blank page from your website. Great idea. Btw google would then also see nothing Commented Oct 31, 2009 at 9:53
  • It's an internal app. It's purely an interface for a java app and it's nice to not have reloads. Commented Nov 5, 2009 at 10:35

3 Answers 3

1

You could simplify the generation by using a framework like MooTools, jQuery, etc...

To generate a form you would have something like this (in MooTools):

var form = new Element('form', { 'id' : 'myFormId', 'class' : 'myFormClass', ... });
var btn = new Element('input', { 'type' : 'button' , ... })
form.inject(btn);

You could encapsulate this logic in some methods for centralizing the code generation

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

2 Comments

how would you do it in jQuery?
var form = $('<form>').attr('id', 'formId').attr('class', 'formClass'); var btn = $('<input>').attr('type', 'button'); form.append(btn); Hope it helps. Cumps.
0

I wrote a project that i created table tr and td using javascript :

objTR = document.createElement("TR");
objTR.setAttribute("id", "factor_row_" + id);

objTD = document.createElement("TD");
objDIV = document.createElement("DIV");

objDIV.setAttribute("id", "title_" + id);
objDIV.innerHTML = title;
objTD.appendChild(objDIV);
objTR.appendChild(objTD);

I think appendChild will work for you. You must just create your form element and it's attributes and then append it to it's parent. Hope it helps you ;)

Comments

0

You can try as well a templating engine like PURE
Define the HTML's and then you render them based on a JSON data.

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.