Avatar of ITsolutionWizard
ITsolutionWizard
Flag for United States of America asked on

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

JavaScriptjQuery

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Leonidas Dosas

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

ASKER
i do not get it
Leonidas Dosas

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ITsolutionWizard

ASKER
your approach does not work at all.
Leonidas Dosas

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
ITsolutionWizard

ASKER
just show me your html code and jquery...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Leonidas Dosas

Ok
Leonidas Dosas

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

ITsolutionWizard

ASKER
not working at all.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Leonidas Dosas

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

ASKER
no matter how many clicks. it does nothing
Leonidas Dosas

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..
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Leonidas Dosas

See below how your code is working:
Capture1.JPG
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.