LoginSignup
7
9

More than 5 years have passed since last update.

bootstrap datepickerを、動的に追加したタグで実行する

Last updated at Posted at 2014-10-04

bootstrap datapickerを使うサンプルは、ほとんどの場合、こんな感じになってます。

index.js
$('#button1').datepicker({
    format: 'dd/mm/yyyy',
    autoclose: true
});

しかし、この方法だと、id='button1'のタグが予め存在している必要があります。
id='button1'のタグを動的に追加した場合では、この方法ではいけません。
以下のように実装します。
ポイントは、showメソッドを明示的に書いている所かと。これが無いと、一度目のクリックでは表示してくれません。

index.js
$('body').on('click','#button1',function(){
    $(this).datepicker(
    {
        format: "yyyy/mm/dd",
        autoclose: true
    });
    $(this).datepicker("show");
})
7
9
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
7
9