I have this form which is used for posting message whose js file is as follows. I want to prevent default action of the form and submit using ajax ::::
function iFrameOn()
{
richTextField.document.designMode = 'On';
}
function iBold()
{
richTextField.document.execCommand('bold',false,null);
}
function iItalic()
{
richTextField.document.execCommand('italic',false,null);
}
function iLink()
{
var linkURL = prompt('Enter the URL for this link:','http://');
richTextField.document.execCommand('CreateLink',false,linkURL);
}
function submit_form(){
var theForm = document.getElementById('myform');
theForm.elements["message"].value = window.frames['richTextField'].document.body.innerHTML;
$('#myform').submit();
$('#myform').submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
console.log($('#myform').attr('action'));
$.ajax({
type:'POST',
url: $(this).attr('action'),
data:formData,
cache:false,
contentType: false,
processData: false,
success:function(data){
console.log(data);
},
error: function(data){
console.log("error");
console.log(data);
}
});
});
}
else
alert("Your message is empty");
}
**THE FORM IS**
<form method="POST" action="dmessage3.php" name="myform" id="myform" class="form1" >
<textarea name="message" id="myTextArea" cols="100" rows="14" style="display:none;"></textarea>
<iframe name="richTextField" id="richTextField" ></iframe>
<div id="toolbar" class="toolbar">
<input type="button" onClick="iBold()" value="B" class="tools" >
<input type="button" onClick="iItalic()" value="I" class="tools">
<input type="button" onClick="iLink()" value="Link" class="tools">
</div>
<input type="hidden" name="id1" value="1">
<input type="hidden" name="id2" value="2">
<input name="myBtn" type="button" value="Submit data" onClick="javascript:submit_form();">
</form>
But the form is not posting using ajax