php - how to change form action using jquery -
im trying change form action based 2 ahref tags, 1 button , other 1 text. when clicks button, action of form should /profile.php?id= , if text clicked action should /profile.php?uid= im stuck @ problem. here code in html:
<a href = "profile.php?/edit/employment-information/" id="edit"><input type="button" name="editemp" id="edit" value="edit"> <a href="profile.php?/add/employment-information/" id="add">add information</a> <form name="empform" method="post" action="profile.php" autofocus id="form"> <input name="employ" type="text" id="employ" pattern="[a-za-z ]{3,20}" placeholder="who employer?"> <input name="position" type="text" id="position" pattern="[a-za-z ]{3,20}" placeholder="what job description?"> <input name="empadd" type="text" id="empadd" pattern="[a-za-z0-9@#$% ,]{5,30}" placeholder="where work address?"> <input name="empcont" type="text" id="empcont" pattern="[0-9]{11}" title="11-digit number" placeholder="contact number">
simple add eventlistner both anchors, each anchor gets own code change form
$(document).ready(function(){ $('#edit').on('click', function(e){ e.preventdefault(); // dont go location of anchor $('#form').attr('action', 'url_if_edit_is_clicked'); // change url }); $('#add').on('click', function(e){ e.preventdefault(); // dont go location of anchor $('#form').attr('action', 'url_if_add_is_clicked'); // change url }); });
if have more anchors, following method might easier: first add class anchors (lets 'formactionchanger'), then:
$(document).ready(function(){ // eventlistener on anchors: $('.formactionchanger').on('click', function(e){ e.preventdefault(); // dont go location of anchor var newaction; // start switch decide new action going used switch(this.id){ case 'edit': newaction = 'url_if_edit_is_clicked'; break; case 'add': newaction = 'url_if_add_is_clicked'; break; default: newaction = 'default_url'; break; } $('#form').attr('action', newaction); // change url }); });
Comments
Post a Comment