-1

I'm trying to grab an username on a MyBB forum, by sending a .get() to the page I hover, then get the username, make it into a variable, and make the hover tooltip show the username.

$('a[href*="forum.net/member.php?action=profile&uid="]').each(function(){
    $.get(this, function(data){
        var username = $(".largetext").text();
    });
    $(this).qtip({
        content: username,
        show: 'mouseover',
        hide: 'mouseout'
    });
});

.qtip() is a plugin I use to make the tooltip popup. It works perfectly, but not when I try this code. $(".largetext").text(); is used to grab the username, once the URL has been loaded.

Anyone know where I'm going wrong? That code will give me an username undefined error, but I tried just to define it to be 0 before the .get() but it'll just be 0 then.

Thanks in advance

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116

1 Answers1

2

Since ajax is asynchronous you can use it like that, in your it can be done as below

$('a[href*="forum.net/member.php?action=profile&uid="]').each(function(){
    var el = $(this);
    $.get(this, function(data){
        var username = $(data).find(".largetext").text();
        el.qtip({
            content: username,
            show: 'mouseover',
            hide: 'mouseout'
        });
    });
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Doing that will give me the usernames on the current page. – MortenMoulder Jul 17 '13 at 10:42
  • That one works perfectly. I forgot to add the $(data) which I use in my function... Thanks a lot, eventhough this got down voted, and was written clearly. Oh well. Have a good day :) – MortenMoulder Jul 17 '13 at 10:46
  • @Snorlax you got down voted maybe because this has been answered thousand times before... But Arun made a great job as usual :) – A. Wolff Jul 17 '13 at 10:51
  • I'm new to jQuery and requests, so this was a challenge for me. Sorry for the stupid question, eventhough I think I explained it pretty well :) – MortenMoulder Jul 17 '13 at 10:53
  • This was not a 'stupid' question, i just explain why it surely been downvoted – A. Wolff Jul 17 '13 at 10:57