Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding a class to dynamically added element.

    I have an element which i add dynamically, then i am trying to add a class to
    that element using jquery's addClass()
    Code:
    $("#tabs [room_id='"+msg_room+"']").addClass("new-msg");
    it works if it's a preloaded element, but not if it was added dynamically.
    I can't use jquery's delegate,live,bind since i have no event, i need to add that class when my ajax call get a specific response.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,701
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    This one line doesn’t tell us anything. Please show us more. Also, if you add the element dynamically within an AJAX function or whatever, it is probably within that function that you need to add the class.

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    More code won't help here.
    $("#tabs [room_id='"+msg_room+"']")
    is selecting an element which has been added dynamically using ajax, and
    I am guessing that's why addclass doesn't work, and that's my problem.

    I have an addition ajax script running each second, when a response triggers
    that function, i need to execute that code, so i can't use
    $("#rooms").delegate('li','click', function() {....
    because i am not clicknig anything or any other event to trigger it.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,701
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    Quote Originally Posted by asmon View Post
    More code won't help here.
    I’m sorry, but how can you determine what helps and what doesn’t? After all, it’s you who wants help from us. If you don’t care to provide any more information even if someone asks for it how can you expect any helpful reply? Am I speaking with myself when I say we need more code? Or do you expect me to use my magic psychic power to read your mind?


  •  

    Posting Permissions

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