-
Dec 5th, 2011, 09:50 AM
#1
Thread Starter
Fanatic Member
[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.
-
Dec 5th, 2011, 12:17 PM
#2
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.
-
Dec 5th, 2011, 01:01 PM
#3
Thread Starter
Fanatic Member
Re: decorating <td> with a class
the extra wrap .....$(data)... that you supplied seems to have done it.
Thanks
-
Dec 6th, 2011, 05:08 PM
#4
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??
-
Dec 6th, 2011, 06:41 PM
#5
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).
-
Dec 6th, 2011, 07:46 PM
#6
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?
-
Dec 7th, 2011, 12:12 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|