1

How do I get the id of the ".item" element from inside the getJSON function?

jQuery(document).ready(function() {

    $(".item").tooltip({
        items: "div",
        content: function(callback) {

            $.getJSON('mydata.json', function(data) {

                var country = event.target.id;

                console.log(country);

            });

        }

    });

});

I've seen an explanation here, but I'm not sure how to pass the event(s) in my case.

Community
  • 1
  • 1

3 Answers3

0

This is how I would do it

jQuery(document).ready(function() {

    $(".item").tooltip({
        items: "div",
        content: function(callback, this) {
                        var $obj = $(this);
            $.getJSON('mydata.json', function(data, $obj) {

                var country = event.target.id;

                console.log(country);
                console.log($obj.attr("id"));

            });

        }

    });

});
Jack Shultz
  • 2,031
  • 2
  • 30
  • 53
0

Try to use the

$(this)

to access the current element, then you can access its ID by using

$(this).attr('id')

See http://jqueryui.com/tooltip/#custom-content

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Adlen Gharbi
  • 214
  • 2
  • 11
0

In your content callback, this refers to the element whose tooltip is being invoked, so you could use this.id to get the id of the current element

jQuery(function ($) {
    $(".item").tooltip({
        items: "div",
        content: function (callback) {
            var el = this;
            $.getJSON('mydata.json', function (data) {
                var country = el.id;
                console.log(country);
            });
        }
    });
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531