We help IT Professionals succeed at work.

jquery, click

ITsolutionWizard
on
Do you know why the first click on button does not work? It actually does not do anything.
However, when i click 2 times, it works.


             <form role="form" class="search-form"  >
                                                <div class="form-group">
                                                    <input required type="email" class="form-control" 
                                                           id="search" name="search" 
                                                           placeholder="Enter your email">
                                                    <button id="gotoNext" name="gotoNext" onclick="fnNext()" class="btn btn-theme" type="button">
                                                        Get Started <i class="fa fa-chevron-right"></i>
                                                    </button>
                                                    <br />
                                                    <label style="color:white;">Your Information is safe & secure</label>
                                                </div>
                                            </form>
                                            <script>
                                                        function validateEmail($email) {
                                                          var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                                                          return emailReg.test($email);
                                                        }

                                                        function fnNext() {
                                                            $('#gotoNext').click(function () {
                                                                var email = $('#search').val();
                                                                var url = '../Quote/BondInfo?search=' + email;
                                                                if (email == '')
                                                                {
                                                                    alert("Invalid Email. Please try again!");
                                                                }
                                                                else if (!validateEmail(email))
                                                                {
                                                                    alert("Invalid Email format. Please try again!");
                                                                }
                                                                else
                                                                {
                                                                    localStorage.setItem('email', email);
                                                                    window.location.replace(encodeURI(url));
                                                                }
                                                            }); 
                                                      }
                                            </script> 

Open in new window

Comment
Watch Question

Distinguished Expert 2017

Commented:
Instead of onclick atrr event in the burron element, remove it and  set at the fnNext the on ('click', function (){........} method

Author

Commented:
i do not get it
Distinguished Expert 2017

Commented:
Remove the onclick =" fnNext()  " attr inside the button tag and at the jquery mode write the following using the on() method ie
$('#gotoNext').on('click',function () {
                                                                var email = $('#search').val();
                                                                var url = '../Quote/BondInfo?search=' + email;
                                                                if (email == '')
                                                                {
                                                                    alert("Invalid Email. Please try again!");
                                                                }
                                                                else if (!validateEmail

Open in new window

Author

Commented:
your approach does not work at all.
Distinguished Expert 2017

Commented:
At the first click you are executing the fnNext() function.To execute the click event you must click a second time.So you must unwrapp the click event from fnNext().If you see the fnNext() the click event is inside at this function but it is not mean that the click event is going to fire with the first click

Author

Commented:
just show me your html code and jquery...
Distinguished Expert 2017

Commented:
Ok
Distinguished Expert 2017

Commented:
HTML:
<form role="form" class="search-form"  >
                                                <div class="form-group">
                                                    <input required type="email" class="form-control" 
                                                           id="search" name="search" 
                                                           placeholder="Enter your email">
                                                    <button id="gotoNext" name="gotoNext"  class="btn btn-theme" type="button">
                                                        Get Started <i class="fa fa-chevron-right"></i>
                                                    </button>
                                                    <br />
                                                    <label style="color:white;">Your Information is safe & secure</label>
                                                </div>
                                            </form>

Open in new window


JQuery script:
function validateEmail($email) {
                                                          var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                                                          return emailReg.test($email);
                                                        }

                                                        
                                                            $('#gotoNext').click(function () {                                                             
                                                                var email = $('#search').val();
                                                                var url = '../Quote/BondInfo?search=' + email;
                                                                if (email == '')
                                                                {
                                                                    alert("Invalid Email. Please try again!");
                                                                }
                                                                else if (!validateEmail(email))
                                                                {
                                                                    alert("Invalid Email format. Please try again!");
                                                                }
                                                                else
                                                                {
                                                                    localStorage.setItem('email', email);
                                                                    window.location.replace(encodeURI(url));
                                                                }
                                                            }); 

Open in new window

Author

Commented:
not working at all.
Distinguished Expert 2017

Commented:
Doesn't work the first click? or not all the code even with the second click?

Author

Commented:
no matter how many clicks. it does nothing
Distinguished Expert 2017

Commented:
What errors do you have at the console ?It's weird because in the test that I have done in the liveweave it's working..
Distinguished Expert 2017

Commented:
See below how your code is working:
Capture1.JPG
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
To answer your question.
The reason you have to click twice is because of the following
Click 1 calls fnNext
function fnNext() {
	$('#gotoNext').click(function () {

Open in new window

This sets up the click handler - doesn't do anything else - just sets up an event handler

Click 2
Click event handler fires - and you get your response
AND
You setup another click event handler

Click 3
Click event handler fires and you get a response
AND
you setup the third click event handler

And so it goes on.

Don't use onlick and click event handlers combined - choose which one you want to use and stick to it so either do this
<button id="gotoNext" name="gotoNext" onclick="fnNext()" class="btn btn-theme" type="button">
  Get Started <i class="fa fa-chevron-right"></i>
</button>

Open in new window

Coupled with this
function fnNext() {
  var email = $('#search').val();
  var url = '../Quote/BondInfo?search=' + email;
  if (email == '')
  {
    alert("Invalid Email. Please try again!");
  }
  else if (!validateEmail(email))
  {
    alert("Invalid Email format. Please try again!");
  }
  else
  {
    localStorage.setItem('email', email);
    window.location.replace(encodeURI(url));
  }
}

Open in new window


OR
<button id="gotoNext" name="gotoNext" class="btn btn-theme" type="button">
  Get Started <i class="fa fa-chevron-right"></i>
</button>

Open in new window

Coupled with this
<script>
$(function() {
  $('#gotoNext').click(function () {
    var email = $('#search').val();
    var url = '../Quote/BondInfo?search=' + email;
    if (email == '')
    {
    alert("Invalid Email. Please try again!");
    }
    else if (!validateEmail(email))
    {
    alert("Invalid Email format. Please try again!");
    }
    else
    {
    localStorage.setItem('email', email);
    window.location.replace(encodeURI(url));
    }
  }); 
});
</script>

Open in new window


But don't mix them