i was searching google just to have way out to generate & submit form by jquery and i found code snippet but few things was not clear to me.
function submitValues(url, params) {
var form = [ '<form method="POST" action="', url, '">' ];
for(var key in params)
form.push('<input type="hidden" name="', key, '" value="', params[key], '"/>');
form.push('</form>');
jQuery(form.join('')).appendTo('body')[0].submit();
}
why [] this third bracket is used like
var form = [ '<form method="POST" action="', url, '">' ];
what is the meaning of the line jQuery(form.join(”)).appendTo(‘body’)[0].submit();
why form.join(”) has been used and why write like appendTo(‘body’)[0] why [0]
please guide me in detail for those above bold syntax. thanks
By questions order:
Brackets
[]define an array. So in the second line it defines array with three items:form.join('')joins all the elements offormarray with delimiter of empty string, soformbecomes a string with concatenated substrings from the array.jQuery(form.join(''))converts your string into DOM elements andappendTo("body")appends these elements (<form><input> ... </form>) insidebody.Using
[0]you get yourform, however not as a jQuery object but as DOM element. Nativeformelement has methodsubmitwhich is called with.submit().For any doubts with native JavaScript methods and functions you can read MDN. jQuery documentation is provided at api.jquery.com.