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?
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?
Share Improve this question edited Mar 25, 2014 at 22:28 Whymarrh 13.6k15 gold badges61 silver badges110 bronze badges asked Mar 25, 2014 at 22:25 s1h4d0ws1h4d0w 7816 silver badges29 bronze badges 2- 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? – C3roe Commented Mar 25, 2014 at 22:41
- Like I said, I know nothing of jQuery or javascript, I simply looked up something that someone else posted. – s1h4d0w Commented Mar 26, 2014 at 15:44
4 Answers
Reset to default 2Try removing the onclick
attribute on all the tr
s instead of only the one being clicked:
$("tr").click(function() {
$("tr").attr('onclick', '');
});
Use the removeAttr function?
$("tr").click(function(){
$(this).removeAttr("onclick");
});
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?
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', '');
});