Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting tooltip text to a div element dynamically

I am trying to set a tooltip text to a container (div) dynamically and using jQuery for each div element (elem.Alias-Status) that I am just adding to the ordered list:

 function addNewElement(elem) {

     var li = $("<li></li>");

     li.prop("class", "ui-state-default");
     li.prop("id", elem.Alias);
     li.text(elem.Name);

     var newItem = '<div id="' + elem.Alias + '-Status" class="elementStatus" tooltipText="' + elem.IP + '"><div class="image"><img id="' + elem.Alias + '-StatusImg" src="@Url.Content("~/images/ongoing.gif")"></div><div id="' + elem.Alias + '-StatusTxt" class="text">Waiting...</div></div>';
     //$('#' + elem.Alias + '-Status').prop('tooltipText', elem. IP);

     li.append(newItem);

     li.appendTo($("#OuterDivContainer"));       
 };

but it is not working obviously. In runtime, when I hover the mouse on each of them, no tooltip is shown. And... I do not know how to do this. I need to create it within this function at the same time item is created.

Above function is called from another function that is iterating over all the items (elements). Then this function pass as parameter elem to addNewElement function.

Any ideas?

I am using jquery-ui 1.10.3 and jquery 1.10.2

like image 947
user304602 Avatar asked Oct 24 '13 16:10

user304602


People also ask

How do I display dynamic content in tooltip?

How do I display dynamic content in tooltip? The tooltip content can be changed dynamically using the AJAX request. The AJAX request should be made within the beforeRender event of the tooltip. On every success, the corresponding retrieved data will be set to the content property of the tooltip.

How do I give a tooltip to a div?

Basic Tooltip HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .

How do I change dynamic value tooltip?

Here's how: Drag the calculated field to the appropriate tooltip and you'll see an ATTR dimension pill with a tooltip logo in the Marks card. Insert the ATTR budget and adjusted inflated gross calculated fields into its corresponding tooltip as seen in Image 6. After that, you're dynamic tooltip should work!

Can tooltips be clickable?

As we mentioned in the preamble, tooltips are completely "mute" to any interactions as to not steal hover events from elements underneath them. Even placing interactive elements, such as links, into content of tooltip will not work.


2 Answers

Works for me:

$(yourElement).prop('title', 'yourText');
like image 115
RoyBS Avatar answered Sep 23 '22 13:09

RoyBS


According the API(http://api.jqueryui.com/tooltip/#option-content), the way to do this in your situation would be to initialize the tooltip after appending the container.

$('#' + elem.Alias + '-Status').tooltip({ content: elem.IP });

Of course, you could also change the tooltip content after initialization like this:

$('#' + elem.Alias + '-Status').tooltip("option", "content", "New Content");

I had this struggle recently and found many other approaches, including the approach you tried, which in your case does not work because the tooltip has not yet been initialized. But this is the correct method and worked for me.

like image 40
user2274784 Avatar answered Sep 21 '22 13:09

user2274784