Sheldon
Sheldon

Reputation: 10095

How do I apply the one handler to different table rows? (jQuery)

I would like to set the html to "" after a key has been pressed (once). This is my code so far:

$(document).one("keypress","td",function(){
        $(this).html("");
});

However, it only works with one table row. Is there away I can apply this to all my table (which is loaded dynamically) rows?

<table class="table">
    <tr>
        <td contenteditable="True">Some text</td>   
    </tr>
    <tr>
        <td contenteditable="True">Some text</td>
    </tr>
</table>

Upvotes: 0

Views: 30

Answers (1)

Kevin B
Kevin B

Reputation: 95066

I would switch to .on and just make the td no longer match the selector after it has been clicked once.

$(document).on("keypress","td:not(.cleared)",function(){
    $(this).empty()/*.html("")*/.addClass("cleared");
});

http://jsfiddle.net/VBHpU/1/

Upvotes: 1

Related Questions