简体   繁体   中英

Appending TR to Table using pure JavaScript without id reference for table

I have a code as follows,

<?php

$actionbutton = '<input type="button" onclick="document.location.href=\'/backoffice_dev.php/currency/edit/EUR\';" value="Edit">';
$tr = '<tr><td>'.$addNewCurrencyPOST["id"].'</td>
<td><div class="led on"></div></td>
<td>'.$addNewCurrencyPOST["symbol"].'</td>
<td>'.$addNewCurrencyPOST["symbolhtml"].'</td>
<td>'.$addNewCurrencyPOST["usdrate"].'</td>
<td>'.$addNewCurrencyPOST["eurrate"].'</td>
<td>'.$actionbutton.'</td></tr>';

?>

<script>
    var tr = <?php echo $tr ?>;
</script>

I need to add the value coming in tr to a table which is already present, i'm preferably looking for a way to use this without insertRow and insertCell . But including these functions is also OK. Note that there is no id for the table, only class name will be available. Any ideas?

You can use something like this in javascript:

tb = document.createElement("tbody")  
var tbody  = document.createElement('tbody'); 
table.appendChild(tbody);
var table_row  = document.createElement('tr');
tbody.appendChild(table_row)//

Suppose you have:

<table id='mytable'>....</table>

You can

<script>
    var tr = <?php echo $tr ?>;
    var table=document.getElementById('mytable');
    table.innerHTML+=tr;
</script>

Edit:

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM