LoginSignup
0
1

More than 5 years have passed since last update.

備忘録:HTMLタグの動的に振られた id 操作

Posted at

下記のように、HTMLタグの id を末尾インクリメントして動的に振るような場合でよくやるTips。

<input type='text' id='inputbox-1' />
<input type='text' id='inputbox-2' />
<input type='text' id='inputbox-3' />
...
$("input").click(function(){
    console.log($(this).attr('id'));
});

'inputbox-1'のように取得できるので、そこから文字列末尾を採取。
その値を他のHTMLタグに紐付けて使用します。

var text = $(this).attr('id');
var suffix = text.substr( text.length -2, text.length );
$('#myModal'+ suffix).modal('show');
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