Elitmiar
Elitmiar

Reputation: 36829

Return false within anchor tag gives error

I have the following statement

<a href="javascript: return false;" id="addNew1">ADD NEW ROW</a>

If I click on this I get the following error in my browser.

return not in function

I use the following JQuery function

$('#addNew,#addNew1').click(function(){

Do I need to include this in the function, the reason I do not add 'n # sign in is because the the page jumps to the top of the page.

Upvotes: 3

Views: 6511

Answers (3)

Šime Vidas
Šime Vidas

Reputation: 185883

This works:

<a href="#" id="addNew1">ADD NEW ROW</a>

$('#addNew, #addNew1').click(function() {
    // do stuff
    return false;
}

Another option would be this:

$('#addNew, #addNew1').click(function(e) {
    // do stuff
    e.preventDefault();
}

Usually, if you want to prevent the default action, which in this case is the activation of the anchor, you do it inside the click handler.

Upvotes: 4

Quentin
Quentin

Reputation: 943134

  1. The href attribute should point to a real URL that will serve the same purpose as the JavaScript if the JavaScript should fail for any reason. Build on things that work.
  2. The click handler should return false so the link isn't followed if the JavaScript doesn't fail (at which point the value of href doesn't matter).

Upvotes: 6

Tom Gullen
Tom Gullen

Reputation: 61729

<a href="#" onclick="return false;" id="addNew1">ADD NEW ROW</a>

Upvotes: 7

Related Questions