SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    RS, Brazil
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [jquery] strange behaviour

    Hello.

    I'm trying to create a JS chatterbot with jquery. User types a message, the message is appended to a DIV with the chat history. Pretty simple. Problem appears when dealing with the scroll. Here's my code:

    Code:
    function myAction() {
    	var what = $('#cmd').val();	
    	$('#cmd').val('');
    	$('#resp').append('<br />$ '+what);
    	// scroll down
    	var o =	$('#resp');
    	o.scrollTop = o.scrollHeight;
    	return false;
    }
    the strange thing is that if I change it to:
    Code:
    ...
    	// scroll down
    	var o =	document.getElementById("resp");
    	o.scrollTop = o.scrollHeight;
    it works perfectly.

    Does anyone have any idea on how could I make it work with jQuery?

  2. #2
    SitePoint Member
    Join Date
    May 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your first code snippet:

    Code:
    	var o =	$('#resp');
    	o.scrollTop = o.scrollHeight;
    o is a jQuery object--that's different than a DOM element. It doesn't have scrollTop and scrollHeight properties

    To get a DOM element you'd do this
    Code:
    	var o =	$('#resp').get(0);
    	o.scrollTop = o.scrollHeight;
    --dave

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats always bugged me in jQuery. In P&S you always have the option of appending [0] as in
    Code:
    var o = $('#resp')[0]
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    RS, Brazil
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you guys. That worked :P

  5. #5
    SitePoint Member
    Join Date
    May 2006
    Location
    Berlin, Germany
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can just append [0] in jQuery as well.


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
  •