By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

Adding <td> to a <table> through Onclick

100+
P: 106
I have a table at which i need to add fields in it whenever the user clicks on the Add button , here's my trial but seems sth is wrong :
Expand|Select|Wrap|Line Numbers
  1. function AddTool()
  2.     {
  3.  
  4.           formdiv = document.getElementById('1');
  5.           formdiv.innerHTML = formdiv.innerHTML +'<td align='center'><a href='#'><img border='0' src='../images/delete.gif'></td> ';
  6.     }
thats the Js function
and thats the table which is generated through looping:
Expand|Select|Wrap|Line Numbers
  1. $query=mysql_query("select * from textures"); while($row=mysql_fetch_array($query))
  2.  {
  3.     echo "<div id='1'"></div>;                                
  4.   echo "<td align='center'>$row[texture_name]</td>";
  5.   echo"<td align='center'><a href='#'><img border='0' src='../images/delete.gif'></td>";
  6.   echo "</tr>";
  7.     }                          
  8.  
any help is greatly appreciated
Nov 5 '08 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
some notes:
- opening <tr> is missing (typo?)
- strictly speaking, "1" is not a valid id and an id must not occur more than once (currently you add the same id to every row)
- more strictly speaking, a <div> belongs into <td> not outside
- if innerHTML is not supported use the DOM methods (createElement(), appendChild(),...), a table row can be accessed as _table_.rows[_row_number_]
- be careful where to add the table cell, it may break your table layout

regards
Nov 5 '08 #2

100+
P: 106
Okay,Now i made it and the new Row is added successfully to the table
but i wonder how can write a JS variable inside HTML

Expand|Select|Wrap|Line Numbers
  1. formdiv.innerHTML = formdiv.innerHTML +'<td align=center>JS VARIABLE</td>'+'<td align=center><a href=#><img border=0 src=../images/delete.gif></td>';
Nov 6 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.