0

Am having the below form in HTML

<form id="test" name="test">
<button id="save" class="small"><i class="icon-plus"></i>save</button>  //KickStart
<button id="edit" class="small"><i class="icon-plus"></i>edit</button>
 <input value=1 type="text" name=Col1 class="Hide">
 <input value=2 type="text" name=Col2 class="Hide">

</form>

JQuery function to be called on button click

$(document).on("click", "#edit", function (e) {

    return false;
});

But when i click on edit button form Submits and not calling the Jquery function as expected.

Maybe I am missing some property of the form ?

9
  • 1
    Can you post your HTML where you've included jQuery? Also, is the event bound within document.ready? Commented Aug 9, 2013 at 6:25
  • Am not getting ur 1st one ! And for second question No ! Separate event not in document.ready Commented Aug 9, 2013 at 6:28
  • You could use the attribute type="button" for the buttons html. This will stop it from submitting. Commented Aug 9, 2013 at 6:31
  • For the 1st one, i mean the HTML code (script tag) where you've referenced jQuery library. As for the second, what @Razz has answered. Commented Aug 9, 2013 at 6:31
  • I replicated your code and do not experience the problem you describe. Commented Aug 9, 2013 at 6:32

4 Answers 4

1

You will have to wait for the DOM to get ready before binding an event to any element.

All you gotta do is put your code inside a

$(document).ready(function(){
    $(document).on("click", "#edit", function (e) {
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend using e.preventDefault(); instead of return false;
And to also use $('#edit').on('click', function(){...}); instead of document.
0

If you have the following code in the header, you need to use $(function(){}). This will run code after load page.

$(function(){
  $("#edit").click(function (e) {
    alert("a");
    return false;
  });
});

Comments

0

try this,

$(document).ready(function(){
     $("#edit").click(function(e) {
         alert("edit button clicked!");
         return false;
     });
});

Comments

0

Another solution is to use the preventDefault() function:

$(document).on("click", "#edit", function (e) {
    alert("Hi");
    e.preventDefault();
});

See http://jsfiddle.net/AbP8Z/ and http://api.jquery.com/event.preventDefault

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.