1

I have a lot of table rows like:

<tr class="player" onclick="document.location = \'fight.php?fightplayer='.$rowselfight['name'].'\';">

All table rows have unique links. I now want to disable all the onclick links after one of them has been clicked on. I tried editing a piece of code I found somewhere else:

$("tr").click(function() {
    $(this).attr('onclick', '');
});

But it doesn't work, does anyone have an idea how to do this?

Whymarrh
  • 11,635
  • 13
  • 55
  • 96
s1h4d0w
  • 743
  • 4
  • 23
  • Just wondering, if you’re already using jQuery, then why are you still using this “old-school” way of event handling via HTML attributes anyway? – CBroe Mar 25 '14 at 22:41
  • Like I said, I know nothing of jQuery or javascript, I simply looked up something that someone else posted. – s1h4d0w Mar 26 '14 at 15:44

4 Answers4

2

Try removing the onclick attribute on all the trs instead of only the one being clicked:

$("tr").click(function() {
    $("tr").attr('onclick', '');
});
Aliou
  • 1,086
  • 10
  • 17
  • This still doesn't work. Using it as: `` I'm trying to avoid people just spam clicking a link. It's for a game where you click a link to "fight". It already only fights once every 3 seconds, but you can just keep clicking and fight once every 3 seconds. – s1h4d0w Mar 26 '14 at 15:47
  • 1
    Could you also try with `$("tr").off('click');` instead of `$("tr").attr('onclick', '');` ? – Aliou Mar 26 '14 at 16:00
  • It actually works, I'm so sorry! The link went to a page that was redirecting back to the page with this javascript on it. So every time it was clicked within 3 seconds it just redirected and could be clicked again. – s1h4d0w Mar 26 '14 at 16:20
1

Use the removeAttr function?

$("tr").click(function(){
    $(this).removeAttr("onclick");
});
Whymarrh
  • 11,635
  • 13
  • 55
  • 96
shuskic
  • 81
  • 2
1

This question has been asked before. You want to remove the event handler from the HTML element. Please refer to: Best way to remove an event handler in jQuery?

Community
  • 1
  • 1
Lee Jenkins
  • 1,804
  • 2
  • 16
  • 34
0

It sounds like you want to remove the onclick from every other element, once the original has been clicked? Or have I misunderstood the question? Try:

$("tr").click(function() {
    $("tr").attr('onclick', '');
});
rorymorris
  • 203
  • 1
  • 4