Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag 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

Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

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

ASKER

i do not get it
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

your approach does not work at all.
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
just show me your html code and jquery...
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

not working at all.
Doesn't work the first click? or not all the code even with the second click?
no matter how many clicks. it does nothing
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..
See below how your code is working:
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial