rogerthat
rogerthat

Reputation: 1815

get table cell data from and place a copy in another table

Two-part question: I have two tables: one table is items and the other is the table that selected items will be copied into when the Add button is clicked. After, when all items that are wanted are selected, there will be a 'finished' button which will post the items selected to the DB.

I started with Javascript but didn't get far at all because jQuery seems better suited but I'm not very familiar with it

 function addto()
 {
var div = document.getElementById('fixedDiv','inside');

div.innerHTML = div.innerHTML + 'Item Added';

 }

<div id='fixedDiv'> 

<table align="center" id="tradee">
    <th rowspan="2"> Other Item</th>


    <?php while($rowi =$item->fetch_assoc())
    {?>

    <tr>
    <td>
        <?php echo $rowi['item_name']; ?>
    </td>
    <td><?php echo $rowi['item_description'];?></td>

    </tr>
    <?php } ?>
</table>

    <br>
        <table align="center" id="tradeTable">
        <th>Cat_id</th>
        <th>Name</th>
        <th>Description</th>
    <?php while($row =$item_results->fetch_assoc())
    {?>

    <tr>
    <td><?php echo $cat_id = $row['cat_id']; ?> </td>

        <td name="item_name"><?php echo $item_id = $row['item_name'];?></td>

        <td><?php echo $item_descrip = $row['item_description'];?></td>

        <td><input name ="submit" type="button" class="added" onclick="addto()" 
        value="Add" >
        </td>

        </tr>
    <?php } ?>

    </table>

Upvotes: 1

Views: 1329

Answers (1)

reyaner
reyaner

Reputation: 2819

i dont know what excatly you try to do, but i hope it helps a bit:

Here is the fiddle with some modifications on your html and jQuery: http://jsfiddle.net/4FArt/5/

function addto(obj)
{
    var $row = $(obj).parents("tr");
    var $table = $("#tradee");
    var item_name = $row.find(".item-name").text();
    var item_desc = $row.find(".item-desc").text();

    var newTR = $("<tr><td>"+item_name+"</td><td>"+item_desc+"</td></tr>");
    $table.append(newTR);

}

You should check also your markup of HTML, a TD has no name Attribute, and ah <TH> is also wrapped by an <TR>... But maybe its just for showing, and you allready know that :)

Upvotes: 1

Related Questions