Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery to wrap a div around a group of td's

Here is the example code I am working on:

<td class="one">content</td>
<td class="two">content</td>
<td class="three">content</td>
<td class="four">content</td>
<td class="five">content</td>

I am able to wrap a div around the first td using:

$('td.one').wrap('<div class="td-one">');

However, I now need to wrap the remaining four td's in a single div...so the desired outcome is:

<div class="td-one">
    <td class="one">content</td>
</div>
<div class="td-two-five">
    <td class="two">content</td>
    <td class="three">content</td>
    <td class="four">content</td>
    <td class="five">content</td>
</div>

How would I do this with jQuery?

like image 816
Eric Avatar asked Aug 18 '12 18:08

Eric


People also ask

How do I wrap a div content?

If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.

What does the jQuery wrap () function do?

jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function. Syntax: $(selector).

How do you wrap two divs?

You can use wrapAll() function!

Which jQuery method allows you to add a wrapper element around another set of elements?

The wrap() method wraps specified HTML element(s) around each selected element.


1 Answers

The convention is to mark each cell with a class which will act as a wrapper given the convenience of jQuery selectors.

<td class="td-one one">content</td>
<td class="td-two-five two">content</td>
<td class="td-two-five three">content</td>
<td class="td-two-five four">content</td>
<td class="td-two-five five">content</td>

Then as needed you can work with groups of cells.

$(".td-two-five").hide();
like image 155
ChaosPandion Avatar answered Sep 22 '22 20:09

ChaosPandion