JavaScript: how to change form action attribute value based on selection?
$("#selectsearch").change(function() {
var action = $(this).val() == "people" ? "user" : "content";
$("#search-form").attr("action", "/search/" + action);
});
If you only want to change form's action, I prefer changing action on-form-submit, rather than on-input-change. It fires only once.
$('#search-form').submit(function(){
var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
$("#search-form").attr("action", "/search/" + formAction);
});
Simple and easy in javascipt
<script>
document.getElementById("selectsearch").addEventListener("change", function(){
var get_form = document.getElementById("search-form") // get form
get_form.action = '/search/' + this.value; // assign value
});
</script>