JavaScript - how to get textbox value inside table element in javascript??

Asked By mani on 21-Dec-11 02:23 AM
hi..i have table that creating multiple dynamic text in each rows..
but i am not able to get value of typing textbox value..
here my code in which iam getting only
object HTMLTableCellElement
of that textbox...but how can i get value from this one..???anyhelp
dipa ahuja replied to mani on 21-Dec-11 02:27 AM
If the textbox is created dynamically then just make sure you are creating it in the page_Load event and access it with the ID which you have assigned to the textbox while creating it
mani replied to dipa ahuja on 21-Dec-11 02:31 AM
i am creating using javascript ..
but when i typing text in any  textbox in any row i van get rowindex value and trying to get textbox vale in that row..but i am getting object HTMLTableCellElement value only..
how to get value of that object HTMLTableCellElement????
Jitendra Faye replied to mani on 21-Dec-11 03:23 AM

You want to get the actual dom node from the table cell instead of the innerHTML (a string). This will allow you to call .value on that node and you're all good. SOmething like:

   tblcells[0].firstChild.value
   // or iterate through children
   var childLength = tblcells.childNodes.length
   for(var i=0;i<childLength;i++){
     alert(tblCells.childNodes[i].value);
   }



Also note that in your code when you're iterating for(var i=1; i<tblrows.length; i++){ you're checking the length of your tblrows array every time which is slow. You should check that length once, as in my code first, then use it in the loop.

kalpana aparnathi replied to mani on 21-Dec-11 03:37 AM
Try this code:

for(var i=2;i<yourTable.rows.length-1;i++)
  {
    if(!isNaN(yourTable.rows[i].cells[4].innerText)&&yourTable.rows[i].cells[4].innerText!="")
    {
      fieldname=parseFloat(yourTable.rows[i].cells[4].innerText);
    }
  }
Anoop S replied to mani on 21-Dec-11 04:14 AM
how to get value of dynamically generated TextBox using javascript

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class placeholderissue : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack)
    {
      if (TextBox1.Text != string.Empty)
      {
        int count = 0;
        int.TryParse(TextBox1.Text, out count);
         
        for (int i = 0; i < count; i++)
        {
          TextBox txt = new TextBox();
          txt.ID = "dynamicText" + i.ToString();
          if (PlaceHolder1.FindControl(txt.ID) != null)
          {
            PlaceHolder1.Controls.Add(txt);
          }
        }
      }
    }
     
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    if (TextBox1.Text != string.Empty)
    {
      int count=0;
      int.TryParse(TextBox1.Text,out count);
      for (int i = 0; i < count; i++)
      {
        TextBox txt = new TextBox();
        txt.ID = "dynamicText" + i.ToString();
        PlaceHolder1.Controls.Add(txt);
      }
    }
  }
}


in html

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="placeholderissue.aspx.cs" Inherits="placeholderissue" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1"
      runat="server" Text="Button" onclick="Button1_Click" />
     
 
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    <input id="Button2" type="button" value="button" onclick="GetTextBoxValue();" />
    <script language="javascript">
      function GetTextBoxValue() {
        var count = document.getElementById("TextBox1").value;
        if (count > 0) {
          for (i = 0; i < count; i++) {
            alert(document.getElementById("dynamicText" + i).value);
          }
        }
      }
    </script>
  <div>
  </form>
</body>
</html>

http://makhaai.blogspot.com/2010/07/get-value-of-dynamically-generated.html