user2171245
user2171245

Reputation: 61

Javascript automatically submit form when input field is not empty

I am looking to see if it's possible to submit a form automatically when a field is not empty. I have a 1 input fielded form which is a date picker. I am hoping that when the user selects a date it submits this value automatically.

Is this possible?

Cheers

I found a solution to it:

    <script>
   function myFunction() { 


var el = document.getElementById('input-datepick').value;
if (el!=""){

     document.getElementById('datepick').submit();

}

}

</script>

then on my HTML

<input onchange="myFunction()" name ="input-datepick" id="input-datepick"       
type="text" class="form-control datepicker">

Upvotes: 1

Views: 1854

Answers (2)

ninetiger
ninetiger

Reputation: 1116

hood a function on date picker enter button.

use jquery .submit function if your input is warped in a form.

Another way could be use .ajax call pass your input value as ajax data. is not needed in this approch

Upvotes: 0

baao
baao

Reputation: 73251

You can simply add an EventListener to your input field and submit the associated form when the input field changes:

document.getElementById('id of your input field').addEventListener('change',function(){
    document.getElementById('id of your form').submit() 
}

Upvotes: 1

Related Questions