-1
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

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Kiran Kumar
  • 127
  • 3
  • 14

2 Answers2

0

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');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • This is a different question but related, what if i want to add input of type file to this form. How do i do it? i was using form.append(""); and was adding value using $('#graph1').val(graph1); How can i do this for file to get something like in the form – Kiran Kumar Jul 19 '16 at 09:35
  • But i get an error that says angular.min.js:62 Error: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string, the file is'nt sent to servlet by putting the name of the file inside value. – Kiran Kumar Jul 19 '16 at 10:24
  • Ah yes, you can only get the value of a file input. You cannot set it at all. This is a security restriction of JS – Rory McCrossan Jul 19 '16 at 10:29
  • For a file input, it's not possible. See this question for more details: http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html – Rory McCrossan Jul 19 '16 at 10:57
0

You can create a single string containing all the necessary markup and inject it using jQuery.

var htmlForm = '<form class="hidden" id="myForm" mrthod="post" enctype = "multipart/form-data" action="myservlet">';
$("body").append(htmlForm);
shivgre
  • 1,163
  • 2
  • 13
  • 29