wootscootinboogie wootscootinboogie - 2 months ago 5x
jQuery Question

Set bootstrap tooltip to AJAX call results

I have a bootstrap tooltip that I want to load data from an AJAX requestion, with the text from the request being the

property of the tooltip. My AJAX request works fine, but I have two problems:

  1. Why isn't the data from the AJAX call making its way into the tooltip?

  2. How can I use my
    object to encapsulate the tooltip's state?

Currently, when the page first loads and I click on
in the console I see
and the correct data from the console.log(ttManager) line

$(document).ready(function () {
//this object's title attribute will be the value of ttManager.title seen below
var ttManager = {
title: '',
setTitle: function (data) {
this.title = data;
var ajaxCall = function () {
//this returns the top five results of text from a query
type: "POST",
contentType: "application/json",
url: "Service.asmx/GetDrugs",
dataType: "json",
success: function (data) {
ttManager.title = data.d;
//inside this function I want to set ttManager.title equal to the data.d

error: function (xhr) {
console.log('failed: ' + xhr.status);



//reference to ajax call
//title is the attribute responsible for displaying text in the tooltip
//I need to use a reusable object to set the text property instead of referencing ajaxCall
//would it be better if there below were title: ttManager.title?
title: ajaxCall,
trigger: 'click',
placement: 'right'


I'm pretty sure that I need a callback function somewhere, but I'm not sure where. Any future pointers would be appreciated, too. Thanks.


First, a little explanation of bootstrap's tooltip plugin. The tooltip displayed will pull from the elements title attribute if there's one present, otherwise it will use the title argument passed.

The next thing you need to understand is that ajax calls are asynchronous. This means code will continue to run while it's waiting for a response. So, for example, if I do something like this

    URL: 'google.com',
    success: function(){

you'd see "woohoo" in the console before "yay". So, currently, you're calling $('#btnSubmit').tooltip before your ajax query has altered the state of ttManager.

The other issue is that you're currently not doing anything with ttManager in relation to bootstrap. I feel like I should also mention that the ttManager object seems meaningless here.

Personally, I would change my ajax success function to this (sets the title attribute, calls tooltip, then produces another click to make the tooltip appear)

success: function(data) {
    $('#btnSubmit').attr('title', data.d)
        trigger: 'click',
        placement: 'right'

remove the current $('#btnSubmit').tooltip... code currently there, and add in a one time click handler to call your ajax.

$('#btnSubmit').one('click', function() {