[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

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
0
Westside2004
Asked:
Westside2004
  • 3
  • 2
1 Solution
 
Kiran Paul VJComputer EngineerCommented:
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
0
 
SirCroftyCommented:
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>
0
 
Westside2004Author Commented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SirCroftyCommented:
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.
0
 
Kiran Paul VJComputer EngineerCommented:
try this, a modified version of SirCrofty's code,
not a good method but it works

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

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

     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><pre>value 2</pre></td>
     </tr>
     </table>

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

hope this helps
kiranvj
0
 
Kiran Paul VJComputer EngineerCommented:
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now