最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Add a 'data-toggle' attribute to a button in javascript - Stack Overflow

programmeradmin4浏览0评论

I'm trying create a bootstrap tooltip in a button in javascript.

This is what I'm trying to achieve:

 <input type="image" src="~/images/icon_trash_red.png" class="m-l-20" onclick="RemoveItem(this)" height="20"  data-toggle="tooltip" data-placement="right" title="Remover aluno" />

This is what I have so far:

    button = document.createElement("Input");
    button.src = "/images/icon_trash_red.png";
    button.width = '20';
    button.type = 'image';
    button.onclick = function () {
       RemoveItem(this);
    };
    button.style.marginLeft = '70px';
    button.title = "Remover aluno";
    button.setAttribute("data-toggle", "tooltip");

The 'setAttribute' seems to be working fine, because if I change to something like this: button.setAttribute("height", "50px"); it works.

Any ideas what I'm doing wrong?

I'm trying create a bootstrap tooltip in a button in javascript.

This is what I'm trying to achieve:

 <input type="image" src="~/images/icon_trash_red.png" class="m-l-20" onclick="RemoveItem(this)" height="20"  data-toggle="tooltip" data-placement="right" title="Remover aluno" />

This is what I have so far:

    button = document.createElement("Input");
    button.src = "/images/icon_trash_red.png";
    button.width = '20';
    button.type = 'image';
    button.onclick = function () {
       RemoveItem(this);
    };
    button.style.marginLeft = '70px';
    button.title = "Remover aluno";
    button.setAttribute("data-toggle", "tooltip");

The 'setAttribute' seems to be working fine, because if I change to something like this: button.setAttribute("height", "50px"); it works.

Any ideas what I'm doing wrong?

Share Improve this question edited Apr 3, 2017 at 15:32 gregoryp asked Apr 3, 2017 at 14:49 gregorypgregoryp 1,0195 gold badges18 silver badges39 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

It will add the attribute to the element but you need to initialize the tooltip.

// initialize tooltip for all elements which has the data-toggle attribute
$('[data-toggle="tooltip"]').tooltip()

// or initialize just for the button
$(button).tooltip()

Note : The code should be after adding the attribute to the element.

发布评论

评论列表(0)

  1. 暂无评论