tags:

views:

42

answers:

3

I have a table layout with the following code

<table cellspacing="0" class="photogalleryTable">
<tbody>
<tr>
<td class="photogalleryItem"><a title="" href="/images/varsity_basketball/alexdefending.jpg" rel="lightbox[12828]" onclick="myLightbox.start(this);return false;"><img border="0" alt="" src="/Utilities/ShowThumbnail.aspx?W=100&amp;H=80&amp;Img=%5c%5c192.168.61.7%5canf01%5cyvir%5cfvgrf1%5c72488%5cSbyqref%5c%5cvzntrf%5cinefvgl_onfxrgonyy%5cnyrkqrsraqvat.wct&amp;USM=1"></a></td>
<td class="photogalleryItem"><a title="" href="/images/varsity_basketball/cheerweb.jpg" rel="lightbox[12828]" onclick="myLightbox.start(this);return false;"><img border="0" alt="" src="/Utilities/ShowThumbnail.aspx?W=100&amp;H=80&amp;Img=%5c%5c192.168.61.7%5canf01%5cyvir%5cfvgrf1%5c72488%5cSbyqref%5c%5cvzntrf%5cinefvgl_onfxrgonyy%5cpurrejro.wct&amp;USM=1"></a></td>
</tr>
</tbody>
</table>

Is it possible to use jQuery to re-format to a unordered list? Somehow convert the td tag to a li tag and remove the rest?

Thanks in advance for any help.

+1  A: 

Sure, something like (untested)

$("table").find("td").each(function(idx,elt){
  $("<li>").html($(elt).html()).appendTo("#target");
});
$("table").remove();

note : you should have a ul somewhere with id 'target', and the selector for the table should be made more precise if there's more than 1 table on the page.

David V.
This wouldn't work - you need to create an li element, not just use a jQuery selector to retrieve them.You would also need to add the "<ul>" element (and the respective closing tag), since it isn't valid to have "li" without "ul" or "ol".
Charles Boyung
@Charles Boyung $("<li>") creates a li element. He also mentions that you would need to have a ui with the id of "target".
PetersenDidIt
@Charles Boyung as petersendidit commented, I do create the li element, and it's not clear from the question if the UL has to be created or if it already exists -- that's why my examples supposes it has an id and adds the items to it.
David V.
I stand corrected - If you make an edit, I will change my vote :)
Charles Boyung
I don't understand what do you want me to edit ? where's the mistake ?
David V.
It won't let me change my vote anymore unless there's an edit. So just add a space or something, so it thinks it has changed, so I can change my vote.
Charles Boyung
oh, didn't know that. edited :)
David V.
+1  A: 

Is the code being generated, e.g. by php? If you do, do you have access to it?

Otherwise, I'd probably strip out each <img /> and build into a <ul>, rather than replace the table code with <ul> and <li>s.

Josh
+1  A: 
var table = $('.photogalleryTable').before('<ul class="photogallery"></ul>')
table.find('td a').wrap('<li>').parent().appendTo('ul.photogallery');
table.remove();
PetersenDidIt
The above code worked perfectly but wondered about one thing. Is it possible to constrain this code to a specific instance of the table? When I have 2 photo galleries on a page, this code takes all of the images and combines them. The result is 2 photo galleries with all the images from each...essentially combining all the images and then duplicating them.Who would I constrain the script to run on a specific gallery instance? I assume it has something to do with a variable .this??Thanks.
mmsa