Search code examples
javascriptcsshtmltwitter-bootstraptooltip

How to retrieve tool-tip text of a button in Bootstrap?


I have the following HTML snippet below. I want to retrieve the tool tip title (78) of the button and use it in another function.

<span class="btn btn-warning btn-sm" data-toggle="tooltip" data-placement="top" title="78" onclick="showToolTipTitle(event);">10:15 AM</span>   

I tried using the following javascript codes to retrieve the tool-tip title but it still doesn't work:

function showToolTipTitle(event)
{   
    var title = $(event.target).attr("tooltip").text;
    alert(title);
}

function showToolTipTitle(event)
{   
    var title = $(event.target).attr("title");
    alert(title);
}

Solution

  • <script>
    $(document).ready(function(){
        $("button").click(function(){
            var new_title = $(this).attr("title");
            alert(new_title);
        });
    });
    </script>
    
    <button data-toggle="tooltip" data-placement="top" title="34">Display Tooltip</button>