0

Can anybody please tell me how to trigger unpress event on Submit button

I am doing a validation on button press if any of the fields are empty . i am preventing th e navigation to second page. But the Submit button remains pressed after validation and if From or To field is left empty. enter code here

        <form action="NewFile.html">
            From:<input id="from" type="text" name="From-busstop">
            To:<input id= "to" type="text" name="To-busstop">
            How do you want to travel?:<Select id="how" name="How">
              <option>Minimum Number of Hops</option>
            <option>Maximum Bus Route Availability</option>
            <option>Via Terminal Bus Stations Only</option>
            <option>Direct Routes Only</option>
            <option>Shortest Distance</option>
            </Select>
            <p>
            <a href="#two" id="submit" data-direction="reverse" data-role="button" data-transition="flip" onClick="loadpage()"
                >Submit"</a>
        </p>
        </form>
        <script type="text/javascript">
        $('#submit').on('click',function(e) {
            var from = document.getElementById('from').value;
            var to = document.getElementById('to').value;
            var how = document.getElementById('how').value;

            if(from.length < 1)
            {
                alert("From bus-stop is empty");
                document.getElementById("from").focus();
                e.preventDefault();
            }
            else if(to.length < 1)
            {
                alert("To bus-stop is empty");
                document.getElementById("to").focus();
                e.preventDefault();
            }
        });
        </script>
  • `.off('click' ,function(){});` Have you tried this, or what's your question... – Michael Jan 14 '15 at 12:41
  • possible duplicate of [cancel a submit in jquery?](http://stackoverflow.com/questions/1773852/cancel-a-submit-in-jquery) – Luizgrs Jan 14 '15 at 12:42
  • The Submit Button remain pressed when i the from / TO field is left empty – mushahid alam Jan 14 '15 at 12:45
  • can you check your console whether any javascript errors. – Hoja Jan 14 '15 at 12:46
  • When I checked your code no errors I could find. http://jsfiddle.net/hoja/LLz469fa/ – Hoja Jan 14 '15 at 12:46
  • Also please check the jquery version you use. – Hoja Jan 14 '15 at 12:47
  • IT gets unclicked automatically if i do validation like this - http://stackoverflow.com/questions/1773852/cancel-a-submit-in-jquery . That is using .Submit function but i was facing different problem with that. So i used click event for validation . But this problem occurs – mushahid alam Jan 14 '15 at 12:49

1 Answers1

0

Change

else if(to.length < 1)

for only

if(to.length < 1)

You want to do two if-conditions, don't you?

If yes, there is no need for else

  • Yes. But that doesnt solve my issue still . My issue is the Button is left pressed. I want to unpress it. Is there a way to do it from Javascript? – mushahid alam Jan 14 '15 at 12:50
  • Try change e.preventDefault() by _return;_ –  Jan 14 '15 at 12:52
  • Great!!!! . It worked. I thought it will work only if we use .Submit validation of Jquery. Thanks a lot!!!:) – mushahid alam Jan 14 '15 at 12:55
  • _return_ is used to exit the function before its normal commands sequence, then in that case, if one element is empty, alert the user and then _return_ the function inside the if-condition. –  Jan 14 '15 at 13:58