Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery .html() strips tr/td tags?

http://jsfiddle.net/WXG7V/

As you can see below, I have a simple div containing a row I would like to append to the end of my table. If I alert the contents of the div back to the screen using ".html()", jQuery strips out all of the td/tr tags.

<html>
<head>
<title>jQuery Strips Table Tags</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {

            alert($("#newRow").html());
            $(".tasks > tbody:last").append($("#newRow").html());
});
</script>
</head>
<body>
    <table class="tasks" style="margin-left: 0px;">
            <tr>
                <th>Feeder ID</th>
                <th>From</th>
                <th>To</th>
                <th># Reels</th>
                <th>Reel Length</th>
                <th>Type</th>
                <th>Colour</th>
            </tr>
        </table>
    </div>

<div id="newRow" style="display: none;">
    <tr>
        <td><input type="text" name="feeder_id" id="feeder_id" /></td>
        <td><input type="text" name="from" id="from" /></td>
        <td><input type="text" name="to" id="to" /></td>
        <td><select name="number" id="number">
            <option value=1>1</option>
            <option value=2>2</option>
            <option value=3>3</option>
            <option value=4>4</option>
            <option value=6>6</option>
            <option value=8>8</option>
            <option value=9>9</option>
            <option value=12>12</option>
            <option value=14>14</option>
            <option value=16>16</option>
        </select></td>
        <td><input type="text" name="length" id="length" /></td>
        <td><select name="type" id="type"><option value="CU">Copper</option><option value="AL">Aluminum</option></select></td>
        <td><input type="radio" name="colour" id="black" value="black" />Black
        <input type="radio" name="colour" id="red" value="red" />Red
        <input type="radio" name="colour" id="blue" value="blue" />Blue
        <input type="radio" name="colour" id="white" value="white" />White
        <input type="radio" name="colour" id="green" value="green" />Green
        </td>
    </tr>
</div>

like image 906
Darrrrrren Avatar asked Sep 24 '11 14:09

Darrrrrren


People also ask

What is the use of TR </ tr tags in HTML?

Description. The HTML <tr> tag defines a row in an HTML table. Each <tr> tag can contain one or more <th> tags that define header cells in the table or one or more <td> tags that define standard cells in the table. This tag is also commonly referred to as the <tr> element.

What is the difference between TR and TD tags?

The tbody tag begins the body (data) area, the same tr tag creates a row, and td tags create data cells in each row.

Can I use TR without TD?

If you don't. Either it won't work, or it will work :-) [based on the DOCTYPE]. If it does work, it's because the browser did the work of inserting the TABLE, TBODY, TR elements into the DOM for you.


1 Answers

<tr>, <td> are only valid elements inside <table>. Use display: table-row and display: table-cell in conjunction with <div> to replace the <tr> and <td>.

Example:

<div class="table">
    <div class="row">
        <div class="cell">
        Foo
        </div>
    </div>
</div>

CSS:

.table{
     display: table;
}
.row {
     display: table-row;
}
.cell {
     display: table-cell;
}
like image 133
Rob W Avatar answered Sep 22 '22 18:09

Rob W