Link to home
Start Free TrialLog in
Avatar of Westside2004
Westside2004Flag for United States of America

asked on

Changing a cell into a text box???

Hi,

I have a query that generates an HTML table of products, and there is an "edit" icon that when clicked basically takes the user to another page where there is a form, in the form I display the data from the table of products with a "Save" Button, so the user can edit the product data like, name, desc, etc, etc hit save, and they are redirected back to the product list.

I am trying to find a way to have the individual cells in my html table become editable.  Basically like an editable grid, you click the edit icon and then the data in th cells would change to an html text input box.  This way the user can essentially stay on the same page as opposed to going on to another page.  Is there a way to do this with CSS and or JavaScript?

Any help appreciated.

Thanks

Malik
Avatar of Kiran Paul VJ
Kiran Paul VJ
Flag of India image

use text area inside cell an show the data in the textarea, and set it as readonly, when the user clicked the edit button remove the readonly property, and edit as usual

code to show textarea with no borders

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<!--
.txtB {
     height: 150px;
     width: 200px;
     border: none;
     overflow:auto;
}
-->
</style>
</head>

<body>
<form name="form1" method="post" action="">
  <textarea name="textarea" class="txtB"></textarea>
</form>
</body>
</html>


kiranvj
Is this what you had in mind?

<html>
<head>
<script language="JavaScript">

function changeTable()
{
      var tags = document.getElementsByTagName("TD");

      for (i = 0; i < tags.length; i++)
      {
            var val = tags[i].innerHTML;
            if (val.indexOf('<') < 0)
            {
                  tags[i].innerHTML = '';
                  var ar = new Array();
                  var newElem = document.createElement("INPUT");
                  newElem.id = 'input' + i;
                  newElem.value = val;
                  tags[i].appendChild(newElem);
            }
      }


}

</script>
</head>
<body>
      <table>
      <tr>
            <td>value 1</td>
      </tr>
      <tr>
            <td>value 2</td>
      </tr>
      </table>

      <input type="button" onclick="changeTable();" value="Edit">
</body>
</html>
Avatar of Westside2004

ASKER

Hi,

This does work SirCrofty, but it changes all td's.  I have an HTML table that has some data I dont want to turn into input fields.

How can I control individual "<td's> essentially.  I have rows of data.  Each row is editable by clicking the edit icon next to that row.  The above code makes all td's into input boxes.

Thanks,

-ws
You could just give each td you want to change an id/attribute and check for that attribute in the loop I suppose, and then change only those tds that have that identifier.
ASKER CERTIFIED SOLUTION
Avatar of Kiran Paul VJ
Kiran Paul VJ
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and if u want to change the font of text inside the pre tags u can use this

<style type="text/css">
<!--
pre {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-size: 12px;
      color: #000000;
      text-decoration: none;
}
-->
</style>

paste it in the head tag.

but here also there is the problem, all pre tags will be converted to text fields,
but pre tags are rarely used in a web page.
kiranvj