Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

JavaScript jQuery class adding help, might squeeze some juices from your mind

views
     
TSchoongyouqi
post Nov 20 2009, 07:52 PM, updated 17y ago

software engineer
*******
Senior Member
2,507 posts

Joined: Jan 2003
From: Malaysia, Asia. Status: admin



I'm trying to add TD's name to their own respective TD's class.

consider the example below:

before:
<td name="test1"></td>
<td name="test2"></td>

after:
<td class="test1" name="test1"></td>
<td class="test2" name="test2"></td>


CODE

var table_tr = $("#table tr");
var table_td = table_tr.children("td");
table_td.addClass($(this).attr("name"));


sample code above is just a rough example which is not a working copy.
Problem lies on the 3rd line, the "this" just not refering to each "td".

any great solution without using any loop? thanks! biggrin.gif


Added on November 20, 2009, 7:57 pmor I may simplify my problem. since table_td carrying an array of TDs, Any way could make me to use $(this) as a reference pointer to each TD object? thanks! biggrin.gif


Added on November 20, 2009, 8:04 pmoh forget it. found my solution. hehe. no person to thanks here. Thanks to myself then. laugh.gif

This post has been edited by choongyouqi: Nov 20 2009, 08:04 PM
chizzu
post Nov 22 2009, 07:08 AM

ブラック★ロックチューター BEAST
******
Senior Member
1,357 posts

Joined: Jan 2003
From: Londinium, Albion
use jquery each to loop.

$(table_td).each(function(){
this.addClass($(this).att('name'));
});

never tested the code but it should work

ah since you found the solution. nvm lol

 

Change to:
| Lo-Fi Version
0.0140sec    0.75    5 queries    GZIP Disabled
Time is now: 10th December 2025 - 02:48 AM