Results 1 to 7 of 7

Thread: [RESOLVED] decorating <td> with a class

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Resolved [RESOLVED] decorating <td> with a class

    Hi All,

    on an Ajax call I have an object returned that holds an HTML Table:

    Code:
    ....success: function (data) {
                    $('#' + section).html(data);
    .....

    This all works fine as it stands and the data renders correctly on the screen.

    However, I want to decorate the <td> elements in data with a class. I thought that doing:

    Code:
    success: function (data) {
                    var ww = $('td', data).addClass("t-hierarchy-cell");
                    $('#' + section).html(ww);
    would do the trick, however none of my <td> elements have the class attribute set.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: decorating <td> with a class

    Works correctly for me as you've written it on jQuery 1.5.2. Are you using an older version?

    Something that I thought might be a problem before trying it was that "data" ought to be a string when it's received by the callback, and that a context provided to the jQuery selector should be a DOM element or a jQuery object. If that were the case, then creating a jQuery object from "data" should remedy it:
    Code:
    var ww = $('td', $(data)).addClass("t-hierarchy-cell");
    $('#' + section).html(ww);
    I guess I'm also making an assumption on the markup contained in "data"; what is the actual value of "data" in your script?
    Last edited by SambaNeko; Dec 5th, 2011 at 12:22 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: decorating <td> with a class

    the extra wrap .....$(data)... that you supplied seems to have done it.

    Thanks

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] decorating <td> with a class

    Wow - I have not been using jQuery 1.5...

    This syntax

    $('td', $(data)).

    wasn't in 1.4 - right???

    Should I be using 1.5 - any reasons not to??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [RESOLVED] decorating <td> with a class

    It's a combination of two constructs (a selector with context and creating a jQuery object from a string of markup) that have been in the framework since 1.0, so it might work in any version, but I'm not sure.

    1.7 is actually the most recent version (as of this post). Personally I tend to upgrade whenever (a) I start a new project and grab the latest version to use from the start or (b) there's a feature I want to use that is newer than my currently-deployed version. The latest versions are often the most optimized versions, and I've never run into major bugs from upgrading (there are version change logs somewhere too of course, if you want/need to check something specific).

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] decorating <td> with a class

    Thanks for the info - I started with the jQuery in Action book and I don't recall seeing it mention that syntax - it's heavily jQuery-in-the-DOM oriented...

    btw - if that is added a class to every td in the table can't that be done with a single "multi-selector parent/child" class assignment to the parent dom-element?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [RESOLVED] decorating <td> with a class

    btw - if that is added a class to every td in the table can't that be done with a single "multi-selector parent/child" class assignment to the parent dom-element?
    Presumably, yes.

Posting Permissions

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



Click Here to Expand Forum to Full Width