开发者

Generating HTML Forms With Javascript

My app uses javascript to generate all the HTML for my website. The index.html basically j开发者_开发知识库ust 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?


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


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 ;)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜