Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to use appendTo in jquery

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:

enter image description here

enter image description here

enter image description here

enter image description here

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;
}
like image 528
user1763812 Avatar asked Oct 19 '15 11:10

user1763812


People also ask

What is the difference between the append () and appendTo () methods?

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.

How append a div in another div using jQuery?

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.

What is prependTo in jQuery?

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.

What is append and prepend in jQuery?

append() & . prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.


1 Answers

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'/>")
like image 88
Pavel Gatnar Avatar answered Oct 02 '22 14:10

Pavel Gatnar