Search code examples
javascriptforms

Submit only non empty fields from Form


That is the form:

<form action="" method="GET">
  <input type="text" name="para1">
  <input type="text" name="para2">
  <input type="submit" value="search">
</form>

Now when I fill out only the first field I get example.com/?para1=value&para2= But I just want it to be example.com/?para1=value because para2 don't contains value. How can I do this? Should be possible with JS or anything?


Solution

  • Try this,

    Include jQuery and use following snippet.

    $(document).ready(function(){
        $("form").submit(function(){
            $("input").each(function(index, obj){
                if($(obj).val() == "") {
                    $(obj).remove();
                }
            });
        });
    });