Alex Rivera | Logout

JQuery create a form and add elements to it programmatically

Asked 2010-12-21T07:39:19.920
35

Hi I need to create a form and add elements to it programatically.

$form = $("<form></form>");
$form.append("<input type=button value=button");

this doesn't seem to work right.

Edit
Report

2 Answers

53

You need to append form itself to body too:

$form = $("<form></form>");
$form.append('<input type="button" value="button">');
$('body').append($form);
answered 2010-12-21T07:42:42.453
30
var form = $("<form/>", 
                 { action:'/myaction' }
            );
form.append( 
    $("<input>", 
         { type:'text', 
           placeholder:'Keywords', 
           name:'keyword', 
           style:'width:65%' }
     )
);

form.append( 
     $("<input>", 
          { type:'submit', 
            value:'Search', 
            style:'width:30%' }
       )
);

$("#someDivId").append(form);
answered 2013-05-20T15:47:08.803

Your Answer