Am I using the jquery appendTo method correctly in the code below?
I am asking because it appears to displays correctly when I test it in jsfiddle but when I use the same code on my local server (in FF, IE and Chrome) it displays with elongated boxes:
I am assuming I am doing something wrong. Thanks.
HTML
<div class="ws-css-table" >
<div class="ws-css-table-tr">
<div class="ws-css-table-td"></div>
<div class="ws-css-table-td"></div>
</div>
<div class="ws-css-table-tr">
<div class="ws-css-table-td"></div>
<div class="ws-css-table-td"></div>
</div>
<div class="ws-css-table-tr">
<div class="ws-css-table-td"></div>
<div class="ws-css-table-td"></div>
</div>
</div>
<br/>
<button id="css-icol">Col +</button><br/><br/>
jquery
$('#css-icol').click(function(){
$(".ws-css-table-td:last").clone().appendTo('.ws-css-table-tr');
var tblArr = $('.ws-css-table > .ws-css-table-tr').map(function ()
{
return $(this).children().map(function ()
{
return $(this);
});
});
lastCol = $('.ws-css-table-tr:first > .ws-css-table-td').length;
for (r=0; r<$('.ws-css-table-tr').length; r++)
tblArr[r][lastCol-1].text('X');
});
css
.ws-css-table {
display: table;
}
.ws-css-table-tr {
display: table-row;
}
.ws-css-table-td {
display: table-cell;
border:1px solid #000;
width: 15px;
height:15px;
text-align:center;
vertical-align:middle;
}
With . append() , the selector expression preceding the method is the container into which the content is inserted. With . appendTo() , on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.
First, select the div element which need to be copy into another div element. Select the target element where div element is copied. Use the append() method to copy the element as its child.
jQuery prependTo() Method The prependTo() method inserts HTML elements at the beginning of the selected elements. Tip: To insert HTML elements at the end of the selected elements, use the appendTo() method.
append() & . prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.
You have an error in this line:
$(".ws-css-table-td:last").clone().appendTo('.ws-css-table-tr');
You select the last cell in the table and clone it to every row. When you want to copy the column, you have to select last cells in all rows, then you have to iterate over the cloned cells. Finally, the method after
is more elegant here...
$(".ws-css-table-td:last-child")
.each(function(){
var $this=$(this)
$this.after($this.clone())
})
To create an empty column use:
$(".ws-css-table-td:last-child")
.after("<div class='ws-css-table-td'/>")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With