0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptで動的に追加した要素(テーブルとか)でクリックイベントが反応しない

Last updated at Posted at 2019-05-28

#JavaScriptで要素(テーブル)を動的に追加した時、テーブルセルのクリック時にクリックイベントが反応しない件

いやーまいった。
悩むこと1時間。いやもっとか。
備忘録として記入


変更前のソース

$(function(){
    $(".hogehoge_table td").click(function(){
        $tag_td = $(this)[0];
        $tag_tr = $(this).parent()[0];
        console.log($tag_tr.rowIndex);  // クリック時の行番号確認用
    });
});

これじゃ、だめらしい。

変更後

$(function(){
    $(document).on('click', '.hogehoge_table td',function(){
        $tag_td = $(this)[0];
        $tag_tr = $(this).parent()[0];
        console.log($tag_tr.rowIndex);// クリック時の行番号確認用
    });
});

clickではなく、onを使って解決。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?