0

i don't understand why a button inside my form can't be reached with my jQuery code.

<form action="">
    <div class="field">
        <label for='username'>Insert username</label>
        <input type="text" id="username" name="username">
    </div>

    <button type='submit' class='btn btn-lg btn-primary buttonRegister'>Register</button>
</form>

Here are my jQuery on click try codes:

$('.buttonRegister').submit(function(){  
    //action inside function
});

--------------OR----------------------

$('.buttonRegister').click(function(){  
    //action inside function
});

--------------OR----------------------

$('.buttonRegister').on("click", function(){  
    //action inside function
});

None of them worked. I'm SURE that code inside click function works because i tried to take button outside the form and it worked well.

4
  • 5
    Because the form submits, reloading the page ? Commented Feb 28, 2015 at 21:27
  • You might want to look into handling the submit on the form. Commented Feb 28, 2015 at 21:29
  • @adeneo i didn't think to that, thanks, i'll need to change it so, cause i need a submit without "refresh" for my php page too.. Commented Feb 28, 2015 at 21:29
  • Your second code looks OK. Add evt to the function args, and do evt.preventdefault() in the function Commented Feb 28, 2015 at 21:30

1 Answer 1

3

Add e.preventDefault() inside before anything else.

$('.buttonRegister').on("click", function(e){  
   e.preventDefault(); // <<-- required to stop the refresh
   //action inside function
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.