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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@Perozzo so what is your question then when the attribute gets added?
Also, make sure you've added the element to the page first before calling tooltip()
@Perozzo: I fyou're doing that before adding the attribute to the button, it won't pick up that button. Once you add the attribute, you need to call $(button).tooltip() to initialize tooltips for that button, since it didn't previously have the attribute. (Don't use Pranav's first example, if you've already done it, no need to re-initialize the others.)
Thank you so much @PranavCBalan @T.J.Crowder @Dave. I was calling $('[data-toggle="tooltip"]').tooltip() just once and before the button's creation. Just added $(button).tooltip(); and it worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.