2

UPDATED

Html Code:

<input type="textbox" id="textbox" value="">
<input type="button" id="btn" value="Validate" onclick="document.write('VALIDATION PASSED')" />​

JS Code:

$(document).ready(function() {
    $("#btn").click(function() {
        if ($("#textbox").val().length == 0) {
            alert("Validation error");        
            return false;
        }
    }).prop("onclick", null );
})​

I have updated my code. So the issue is that after first click my onclick event stopped working. How I could fix it?

P.S. Please DO NOT change html code. I have some reasons to ask about it, but please could you please do it only with javascript? I realize that probably this is not the most easy way but I have some technical limitations in my application.

Thanks!

JSFiddle http://jsfiddle.net/B5GWx/12/

4 Answers 4

7
$(document).ready(function() {
    $("#btn").click(function() {
        alert("Want to show only this message");        
        return false;
    }).prop("onclick", null );
})​

http://jsfiddle.net/B5GWx/5/

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, sorry, my mistake. Your solution really helped me. Thanks.
2

You can't reliably, cross-browser, use a DOM2 handler to prevent a DOM0 handler from running.

What you can do, though, is remove the DOM0 handler entirely:

$(document).ready(function() {
    var btn = $("#btn");
    btn.click(function() {
        alert("Want to show only this message");        
        return false;
    });
    btn[0].onclick = null;   // <==== Here
})​;

Comments

1

Just removing the DOM handler will do the job. No need to return false; or e.preventDefault();

$(document).ready(function() {
    $("#btn").click(function(e) {
        alert("Want to show only this message");        
    }).prop("onclick", null );
})​

DEMO

2 Comments

There were two previous answers saying this when you posted this answer (each slightly different, posted almost simultaneously). Surely in the more than 10 minutes since those were posted, you could see this was just a duplicate answer. Having lots of duplicate answers just increases clutter.
Agree but all of the answers are including return false; or e.preventDefault(). I wanted to highlight this point.
-3

You are looking for preventDefault

Description: If this method is called, the default action of the event will not be triggered.

$(document).ready(function() {
    $("#btn").click(function(e) {
        alert("Want to show only this message");        
        e.preventDefault();
    })
})​

3 Comments

wrong on 3 counts: the onclick directly on the element executes before the jQuery handler, returning false from a jQuery handler already prevents default and prevent default doesn't stop other handlers from executing in the first place
Returning false from a jquery click handler already does prevent default for you.
DOM0 handlers (onxyz) are executed before DOM2 handlers on some engines. And preventing the default action doesn't necessarily prevent them on others. Stopping propagation might, on some. But what you really need to do is remove the DOM0 handler.

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.