0
$(function() {
   $.get('/viewonline',function(data) {
      var members = $('a.username');
      var location = $('a.location');
      var elems = $(data);
for (var i=0; i<members.length;i++) {
   var uname = members[i].text();
   var aname = members[i].href;
 $('.userlist_online').html('<a href="'+ aname +'">'+ uname +'</a>');
}
for (var j=0;j<location.length;j++) {
   var locaname= location[j].text();
   var locaaname = location[j].href;
 $('.userlist_views').html('<a href="'+ locaaname +'">'+ locaname +'</a>');
  }
 },'html');
});

The code above I am trying to loop through all the a hrefs with classes of username and location. So the code above doesn't throw any errors yet it is not posting any data into the divs.

Any one can help me figure out what I did wrong?

4
  • Can you possibly jsfiddle this so I can see the HTML that accompanies it? Mar 28, 2013 at 2:36
  • 1
    Problems here: (1) data is not used anywhere. It is wrapped as elems, but elems is not used anywhere. (2) You're calling $('.userlist_online').html(..) in a loop. Every time you call that, it replaces the entire contents of .userlist_online - it doesn't append to what's already there. (3) You're doing members[i].text();, but members[i] is not a jQuery object so it doesn't have the text() method. I'm surprised this doesn't throw an error, but maybe members is empty so the line never executes.
    – tcovo
    Mar 28, 2013 at 2:45
  • you should use $(members[i]).text() and $(location[j]).text()
    – George
    Mar 28, 2013 at 2:46
  • @tcovo yes that is what I am saying, I was getting an object error then messed around with the code more and it never threw a error. So this is why I am confused now. and my vars should be outside of the loop? @George ok so don't use the var locaname =$(location[j]).text() instead? Sorry guys not very good at .get() and loops. the elems was first used for this $('.userlist_views').html(elems.find(location[i])); So not sure
    – EasyBB
    Mar 28, 2013 at 2:52

1 Answer 1

2

Here's my guess at what will work:

$(function () {
    $.get('/viewonline', function (data) {

        data = $(data);
        var members = data.find('a.username');
        var location = data.find('a.location');

        for (var i = 0; i < members.length; i++) {
            var uname = $(members[i]).text();
            var aname = members[i].href;
            $('.userlist_online').append('<a href="' + aname + '">' + uname + '</a>');
        }
        for (var j = 0; j < location.length; j++) {
            var locaname = $(location[j]).text();
            var locaaname = location[j].href;
            $('.userlist_views').append('<a href="' + locaaname + '">' + locaname + '</a>');
        }
    }, 'html');
});

Fixes done:

  • get members and location by finding elements in the returned data, not in the current document.
  • wrap Elements with $() before calling text()
  • use append instead of html when filling the lists
1
  • Worked Great Actually! Thank you and I loved the notes on what you did. I really appreciate it!
    – EasyBB
    Mar 28, 2013 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.