html - Submitting Codemirror data with JavaScript not working correctly -
how can prevent needing submit form twice when using codemirror , submitting form javascript. happens is, changes make in codemirror textarea, stored temporarily in textarea (it has no name or id). if click submit button twice correct data sent avoid having click twice. here source code submit form:
var ajax; var formdata = new formdata(); var fileupload = false; (i = 0; < e.elements.length; i++) { if (e.elements[i].type == "submit") { if (submitname == e.elements[i].name) { if (submit != null && submit.disabled) submit.disabled = false; submit = e.elements[i]; submit.disabled = true; formdata.append(e.elements[i].name, e.elements[i].value); } } else if (e.elements[i].type == "radio" || e.elements[i].type == "checkbox") { if (e.elements[i].checked) formdata.append(e.elements[i].name, e.elements[i].value); } else if (e.elements[i].type == "file") { abortbutton.disabled = false; var file = document.getelementsbyname("file")[0].files[0]; var maxsize = 1024; var maxsizebytes = 1048576 * maxsize; formdata.append("archivo", file); fileupload = true; } else { //alert(e.elements[i].type + e.elements[i].name + e.elements[i].class + e.elements[i].value); if (e.elements[i].name == 'code') { alert(e.elements[i].type + e.elements[i].name + e.elements[i].value + e.elements[i + 1].value); formdata.append(e.elements[i].name, e.elements[i].value); //formdata.append(e.elements[i].name, e.elements[i].value + e.elements[i + 1].value); } else formdata.append(e.elements[i].name, e.elements[i].value); } } formdata.append("javascript", "javascript"); status = e.elements[0].value.split('/').reverse()[0] + "-status"; ajax = new xmlhttprequest(); if (!fileupload) { ajax.addeventlistener("load", manageload, false); ajax.addeventlistener("error", manageerror, false); } else { ajax.upload.addeventlistener("progress", manageprogress, false); ajax.addeventlistener("load", _manageload, false); ajax.addeventlistener("error", _manageerror, false); ajax.addeventlistener("abort", _manageabort, false); globalajax = ajax; } ajax.open("post", e.action); ajax.send(formdata); return false;
nb. line of code: if (e.elements[i].name == 'code')
determines element codemirror textarea.
i looking non-jquery solution. thanks.
you may want consider using cm.getdoc().getvalue()
instead of reading value directly dom element (e.elements[i].value)
).
(cm
reference instantiated codemirror object, in var cm = codemirror(...)
)