Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2004, 07:03 AM
Registered User
 
Join Date: Aug 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DoTheLoop
Default How do I change innerHTML on a particular row's ce

Hia!

I have a question about tables and changing the contents of a particular row inside that table.

Something like this:
<table id=mytable>
  <tr>
    <td><input type=button name=foo value=click onclick=javascript:changethisrow(andthesecells);></td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
</table>

What I want to put into those particular cells looks something like this:
<tr>
  <td><input type=text name=something_unique value=foo></td>
  <td><input type=text name=something_unique value=foo></td>
  <td><input type=text name=something_unique value=foo></td>
  <td><input type=text name=something_unique value=foo></td>
</tr>

Thanks in advance

Regards
DoTheLoop


 
Old August 23rd, 2004, 10:05 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This may help you get started:
Code:
<html>
<head>
<title>Table Editor</title>
<script type="text/javascript">
function makeRowEditable(Row)
{
  var iRowIndex = Row.rowIndex;
  for (var i = 1; i < Row.cells.length; i++)
  {
    var oCell = Row.cells[i];
    var sNewName = "txt_R" + iRowIndex + "_C" + i; 
    makeCellEditable(oCell, sNewName, oCell.innerText);  //Change second parameter to something else if you want different value in textbox.
  }
}

function makeCellEditable(Cell, NewName, InitialValue)
{  
  var oInput = document.createElement("<input name=\"" + NewName + "\" type=\"text\">");
  oInput.value = InitialValue;
  oInput.size = 3;
  Cell.removeChild(Cell.childNodes[0]);
  Cell.appendChild(oInput);  
}

</script>
</head>
<body>

<table size="80%" align="center">
  <tbody>
    <tr>
      <td width="40%"><input type="button" value="Edit Row" onclick="makeRowEditable(this.parentElement.parentElement);"></td> 
      <td width="20%">1</td>
      <td width="20%">2</td>
      <td width="20%">3</td>
    </tr>
    <tr>
      <td><input type="button" value="Edit Row" onclick="makeRowEditable(this.parentElement.parentElement);"></td>
      <td>4</td>
      <td>5</td>
      <td>6</td>
    </tr>
  </tbody>
</table>
</body>
</html>
--

Joe (Co-author Beginning XML, 3rd edition)
 
Old August 23rd, 2004, 10:34 AM
Registered User
 
Join Date: Aug 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DoTheLoop
Default

Thanks a bunch.

Real neat and tidy.

Regards
DoTheLoop






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using innerHTML adfranklin BOOK: Professional Ajax 2nd Edition ISBN: 978-0-470-10949-6 0 April 13th, 2007 01:13 AM
Deleting row's g_vamsi_krish SQL Server 2000 5 March 7th, 2006 09:20 AM
connect sql server in windows ce / ce .net bigman VS.NET 2002/2003 1 June 9th, 2005 04:11 PM
Getting InnerHtml ghari Javascript 3 January 3rd, 2005 12:06 PM
innerHTML interrupt Javascript How-To 6 July 22nd, 2004 02:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.