Kiran Kumar Kiran Kumar - 4 months ago 21
jQuery Question

Add enctype in jquery form

var form = $('<form/>', {
action: 'SupportRequest',
method: 'POST',
name: 'myForm',
id: 'myForm'
}).appendTo('body');


I have to add
enctype="multipart/form-data"
manually during form creation, how do i do this so that the above code looks like

<form class="hidden" id="myForm" mrthod="post" enctype = "multipart/form-data" action="myservlet">


and also please tell me how to make it of class='hidden' manually

Answer

You can just add them to the parameters of the object you already provide:

var form = $('<form/>', {
    action: 'SupportRequest', // this should be 'myservlet' to match the HTML you gave
    method: 'POST', 
    name: 'myForm', 
    id: 'myForm',
    enctype: 'multipart/form-data',
    class: 'hidden'
}).appendTo('body');