Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a javascript function using Twitter´s Bootstrap?

I want to use the javascript provided in Tritter´s Bootstrap. I´ve checked that all the .js files are pointed out ok.

I want to test if it works ok, so after reading the docs, I understand that I have to "call" the appropiate javascript function?

Using bootstrap-tooltip.js Trigger the tooltip via javascript: $('#example').tooltip(options)

How may I call that function? It seems that it´s not enough to put the proper html, I have to call the function aswell?

Thanks for your help!!

like image 982
Rosamunda Avatar asked Jan 18 '23 10:01

Rosamunda


1 Answers

Give a HTML element the ID of example and appropriate attributes like this:

<p><a href="#" rel="tooltip" title="first tooltip" id="example">hover over me</a>,/p>

In your <head> create a JavaScript <script> element, make sure jQuery is loaded and then, within the jQuery, add the tooltip to the element with the ID of example:

<script type="text/javascript">
    window.onload = function()
    {
        if(!window.jQuery)
        {
            alert('jQuery not loaded');
        }
        else
        {
            $(document).ready(function(){
                $('#example').tooltip({'placement':'top', 'trigger' : 'hover'});
            });
        }
    }
</script>

This will setup a tooltip when you hover over the #example element. Change top to left, right or bottom to change where the tooltip is placed in relation to the element itself.


In case you might want to in the future, this is how you'd set up a popover also.

like image 155
Adam Lynch Avatar answered Jan 31 '23 07:01

Adam Lynch