jquery, click

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

LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
Instead of onclick atrr event in the burron element, remove it and  set at the fnNext the on ('click', function (){........} method
ITsolutionWizardAuthor Commented:
i do not get it
Leonidas DosasCommented:
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

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ITsolutionWizardAuthor Commented:
your approach does not work at all.
Leonidas DosasCommented:
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
ITsolutionWizardAuthor Commented:
just show me your html code and jquery...
Leonidas DosasCommented:
Ok
Leonidas DosasCommented:
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

ITsolutionWizardAuthor Commented:
not working at all.
Leonidas DosasCommented:
Doesn't work the first click? or not all the code even with the second click?
ITsolutionWizardAuthor Commented:
no matter how many clicks. it does nothing
Leonidas DosasCommented:
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..
Leonidas DosasCommented:
See below how your code is working:
Capture1.JPG
Julian HansenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.