[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.
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?
Re: decorating <td> with a class
the extra wrap .....$(data)... that you supplied seems to have done it.
Thanks
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??
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).
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?
Re: [RESOLVED] decorating <td> with a class
Quote:
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.