I have an HTML form and i want to submit it using a link. The form has to be sent to the href
of the link. This is my idea:
Set a click event to the link tag
which does the following:
- Blocks the default action of following the link.
- Edits the form by setting the action to the href of the link.
- Submits the form using jquery submit() function.
I am looking for a solution where i don't have to change my html, only javascript.
I tried this, but it doesn't work. Here is my code:
Javascript:
jQuery('.row_header').find('td').each(function(){
var cell = jQuery(this);
cell.find('a').each(function(){
//Get last link in cell. There is only one
linkObject = jQuery(this);
});
//Gets form
var form = jQuery('#searchForm');
if(typeof linkObject !== 'undefined'){
//Get location url for form
var url = linkObject.attr('href');
linkObject.click(function(e){
//Prevent default (following link)
e.preventDefault();
//Set location from link as form action
form.attr('action',url);
//Submits form
form.submit();
});
}
});
HTML:
<form id="searchForm">
<input type="text" name="myInput" value="myValue">
<input type="submit" name="mySubmit" value="mySubmitValue">
</form>
<table>
<tr class="row_header"><td><a href="myLocation.php">The Link </a></td></tr>
</table>