SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JQuery Function Trouble

    I'm having a problem getting this to work with return all the data at once from the each portion.

    Can anyone see what I have done wrong?

    Code JavaScript:
    $.fn.LoadMessages = function() {
    	$.getJSON('/ajaxListMessages/4', function(data)
    	{
    		this.each(data, function(key, val) {
    			$(area).append(data[key].login + data[key].message + '<br />');
    		});
    	});
    }
     
    $(document).ready(function() {
    	$("#MSG").LoadMessages();
    });




    If I do it this way it works, but i dont want to do it this way because i have to specify the same thing twice!

    Code JavaScript:
    $.fn.LoadMessages = function(varx) {
    	$.getJSON('/ajaxListMessages/4', function(data)
    	{
    		$.each(data, function(key, val) {
    			$(varx).append(data[key].login + data[key].message + '<br />');
    		});
    	});
    }
     
    $(document).ready(function() {
    	$("#MSG").LoadMessages("#MSG");
    });

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    It sounds like you're wanting the .selector method.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply PMW.

    I read your post, that was an interesting function I haven't played with. I then looked over some JQuery plugin development areas and found the culprit!

    It has to do with "this" (not the same as $(this) ) -- I assumed that the "this" of a function behaved differently than $(this) -- okay now this is sounding really confusing...

    Anyways, this being nested inside the "getJSON", was applying to the JSON item, so I need a variable to maintain the DOM element inside it by creating a variable.

    Solution:
    Code JavaScript:
    $.fn.LoadMessages = function()
    {
    	var rootThis = this;
    	$.getJSON('ajaxListMessages/5', function(data)
    	{
    		$.each(data, function(key, val) {
    			rootThis.append(data[key].login + data[key].message + '<br />');
    		});
     
     
    	});
     
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •