Chethan
Chethan

Reputation: 99

Avoid posting back when user clicks html button

I want to avoid posting back when user clicks an html button. because i want show one pop up using javascript. I dont want postback when the button is clicked.

function ShowEditChoicesPopup() {
    // I want to show some pop up here.
    $("popupdiv").show();
    return false;
}

And the markup am using is:

<button onclick="javascript:ShowEditChoicesPopup();"> Edit Choices </button>

Please suggest some idea.

Upvotes: 0

Views: 62

Answers (5)

Jayesh Goyani
Jayesh Goyani

Reputation: 11154

Try with the below code snippet.

<button onclick="javascript:return ShowEditChoicesPopup();">

OR

<button onclick="javascript:return ShowEditChoicesPopup(); return false;">

OR

<button onclick="javascript:ShowEditChoicesPopup(); return false;">

Upvotes: 0

Follow the example:

http://jsfiddle.net/guinatal/ct8uS/1/

HTML

<form>
    <button onclick="return ShowEditChoicesPopup();"> Edit Choices </button>
    <div id="popupdiv" style="display:none">popupdiv</div>
</form>

JS

function ShowEditChoicesPopup() {
     // I want to show some pop up here
     $("#popupdiv").show();

    return false;
}

Upvotes: 0

Satz
Satz

Reputation: 105

Use event.preventDefault(); which will prevent default behaviour of the element and performs specified function

Upvotes: 0

Murali Murugesan
Murali Murugesan

Reputation: 22619

Apply event.preventDefault();

 function ShowEditChoicesPopup(event) {
   event.preventDefault();

    $("popupdiv").show();

 }

Upvotes: 1

Jason Evans
Jason Evans

Reputation: 29186

Try:

function ShowEditChoicesPopup() {
     // I want to show some pop up here
     $("popupdiv").show();
        return false;
     }
    return false;
}

You need a second return false for ShowEditChoicesPopup() which should cancel form submission. This assumes the code is in a <form> block.

Upvotes: 0

Related Questions