473,765 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieve values into JavaScript from a Table

gchq
96 New Member
Hi there

Here is the situation - a table is built dynamically with values in the cells I need to retrieve and enter into a database. I have found a way of getting values using JavaScript, but of course with the table being dynamic I have no idea how many rows it will contain. One way is using the TableID.rows.co unt and then building if statements based on the value - but that is going to be an awfull lot of coding...... So the question is - is there an easier way to retrieve the values? I know that building an array is the answer, but actually getting from A to B is giving me the headache. Any pointers would be appreciated!

Here is the JavaScript that works - but has the limits above. Any ideas would be appreciated!

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim GetValues As String = "<script language='JavaScript'>" & _
  3.                         "var table= document.getElementById('Table1'); " & _
  4.                         "var nRows = new Array(); " & _
  5.                         "nRows[0] = table.rows[2]; " & _
  6.                         "nRows[1] = table.rows[3]; " & _
  7.                         "nRows[2] = table.rows[4]; " & _
  8.                         "nRows[3] = table.rows[5]; " & _
  9.                         "nRows[4] = table.rows[6]; " & _
  10.                         "nRows[5] = table.rows[7]; " & _
  11.                         "nRows[6] = table.rows[8]; " & _
  12.                         "nRows[7] = table.rows[9]; " & _
  13.                         "nRows[8] = table.rows[10]; " & _
  14.                         "nRows[9] = table.rows[11]; " & _
  15.                         "var ID1 = nRows[0].cells[0]; " & _
  16.                         "var ID2 = nRows[1].cells[0]; " & _
  17.                         "var ID3 = nRows[2].cells[0]; " & _
  18.                         "var vID1 = ID1.innerText; " & _
  19.                         "var vID2 = ID2.innerText;" & _
  20.                         "var vID3 = ID3.innerText; " & _
  21.                         "var vQty1 = document.getElementById('ctl00_txt0').value ; " & _
  22.                         "document.getElementById('Label1').innerText = vID1 ; " & _
  23.                         "document.getElementById('Label2').innerText = vID2 ; " & _
  24.                         "document.getElementById('Label3').innerText = vID3 ; " & _
  25.                         "document.getElementById('Label4').innerText = vQty1 ; " & _
  26.                         "</script>"
  27.  
  28.             GetValues = GetValues.Replace("Table1", Table1.ClientID).Replace("Label1", Label1.ClientID).Replace("Label2", Label2.ClientID).Replace("Label3", Label3.ClientID).Replace("Label4", Label4.ClientID)
  29.             Page.ClientScript.RegisterStartupScript(Me.GetType(), "GetValues", GetValues)
  30.  
  31.  
Jan 6 '07 #1
7 21480
acoder
16,027 Recognized Expert Moderator MVP
Define a tbody under your table that encapsulates your TRs and TDs.

Then, assuming an id of "tbody", use the following code to retrieve the current row count:
Expand|Select|Wrap|Line Numbers
  1. var currRow = document.getElementById("tbody").rows.length;
Jan 6 '07 #2
acoder
16,027 Recognized Expert Moderator MVP
Actually, I'm not sure if defining tbody is necessary, but it works for me. Give it a shot.
Jan 6 '07 #3
gchq
96 New Member
I can get the rows simply by :-

Expand|Select|Wrap|Line Numbers
  1. var totalrows = table.rows.length;
The problem I've got is how to define the array (once the total number of rows are known) and then get the variables into a database.

BTW, the first two rows are only headers and don't contain data, and the last row contains a total in cell 3.
Jan 6 '07 #4
acoder
16,027 Recognized Expert Moderator MVP
Why don't you just define the array when constructing the table in your ASP (I presume you're using ASP?) code. You can ignore the first two rows when populating the array with values. Then you can also have a variable which stores the total value that you mention. The database problem is totally ASP-related, nothing to do with javascript.
Jan 6 '07 #5
gchq
96 New Member
Having re-read my initial posting, it does seem rather ambiguous. I apologise for that.

Here is a better description of the dynamic table:-

Row 0 is the header
Row 1 contains description in 6 columns
Row 2 onwards contain the dynamic rows. Cell 2 contains an <input> textbox (not a TextBox Control).
The final row contains a total in cell 3

Under the me.load event I then need to run a script to first find how many rows, then get those rows into an Array, then manipulate the data from that array (update a database, send to another page..)

I do know that by using:-

var table= document.getEle mentById('Table 1');

I can get the table rows and cells, and by using

var totalrows = table.rows.leng th; (Thanks acoder :-) )

I can get the total number of rows

I also know that

var nRows = new Array();
nRows[0] = table.rows[2]


defines the first set of values in the first column (I can retrieve the other values back from the database once I have the ID number from this column)

and that I can get the value of the first TextBox (that gives the quantity) with

vQty1 = document.getEle mentById('txt0' ).value ;

each textbox in the row has an incremented ID value - txt0, txt1, txt2 and so on

Where I hit the buffers is formatting an array to hold the data, then finding a way of retrieving it again!

I have come across the following snippet :-

for(var i = 0; i < x.x.length; i++)

Whilst I know there is a key there somewhere,,,, sigh
Jan 6 '07 #6
gchq
96 New Member
Why don't you just define the array when constructing the table in your ASP
The main problem is the table is fomed in a .NET MasterPage - the processing page is the content. .NET loads the processing page first, then the masterpage, so it's impossible to capture the variables in VB - since JavaScript runs AFTER the full page load (and I can do that in VB code behind) I will get the variables that have loaded AFTER the processing page. (Otherwise VB will always return a value of 3 for the rows (the two headers and the footer).
Jan 6 '07 #7
gchq
96 New Member
This seems rather messy, but it gets values up to a max. pre-defined amount of rows into a container to process!

If anyone knows an easier way (and I'm sure there is) I'd love to know about it!

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Dim vTotalRows As Integer = Request.QueryString("vID")
  4.         HiddenField1.Value = CStr(Request.QueryString("total"))
  5.         Dim Table1 As HtmlTable = Master.FindControl("Table1")
  6.         Dim vValue1 As String = CType(Me.Page.Master.FindControl("TextBox3"), TextBox).Text
  7.         TextBox3.Text = vValue1
  8.  
  9.         If vTotalRows > 3 Then
  10.             'start JavaScript
  11.             Dim GetValues As String = "<script language='JavaScript'>" & _
  12.             "var table= document.getElementById('Table1'); " & _
  13.             "var totalrows = table.rows.length; " & _
  14.             "var i = (totalrows - 3) ; " & _
  15.             "var nID = new Array(i); " & _
  16.             "nID[0] = table.rows[2].cells[0].innerText; " & _
  17.             "var vQty1 = document.getElementById('ctl00_txt0').value ; " & _
  18.             "if (i > 1) { " & _
  19.             "nID[1] = table.rows[3].cells[0].innerText; " & _
  20.             "var vQty2 = document.getElementById('ctl00_txt1').value ; " & _
  21.             "}" & _
  22.             "else { " & _
  23.             "nID[1] = ''; " & _
  24.             "var vQty2 = '' ;" & _
  25.             "}" & _
  26.             "if (i > 2) { " & _
  27.             "nID[2] = table.rows[4].cells[0].innerText; " & _
  28.             "var vQty3 = document.getElementById('ctl00_txt2').value ; " & _
  29.             "}" & _
  30.             "else { " & _
  31.             "nID[2] = '' ;" & _
  32.             "var vQty3 = '' ;" & _
  33.             "} " & _
  34.             "if (i > 3) { " & _
  35.             "nID[3] = table.rows[5].cells[0]; " & _
  36.             "var vQty4 = document.getElementById('ctl00_txt3').value ; " & _
  37.             "} " & _
  38.             "else { " & _
  39.             "nID[3] = '' ; " & _
  40.             "var vQty4 = '' ; " & _
  41.             "}" & _
  42.             "if (i > 4) { " & _
  43.             "nID[4] = table.rows[6].cells[0]; " & _
  44.             "var vQty5 = document.getElementById('ctl00_txt4').value ; " & _
  45.             "} " & _
  46.             "else { " & _
  47.             "nID[4] = ''; " & _
  48.             "var vQty5 = '' ; " & _
  49.             "} " & _
  50.             "if (i > 5) { " & _
  51.             "nID[5] = table.rows[7].cells[0]; " & _
  52.             "var vQty6 = document.getElementById('ctl00_txt5').value ; " & _
  53.             "} " & _
  54.             "else { " & _
  55.             "nID[5] = '' ; " & _
  56.             "var vQty6 = '' ; " & _
  57.             "}" & _
  58.             "if (i > 6 ) { " & _
  59.             "nID[6] = table.rows[8].cells[0]; " & _
  60.             "var vQty7 = document.getElementById('ctl00_txt6').value ; " & _
  61.             "} " & _
  62.             "else { " & _
  63.             "nID[6] = '' ; " & _
  64.             "var vQty7 = '' ; " & _
  65.             "}" & _
  66.             "document.getElementById('Label1').innerText = nID[0] ; " & _
  67.             "document.getElementById('Label2').innerText = nID[1] ; " & _
  68.             "document.getElementById('Label3').innerText = nID[2] ; " & _
  69.             "document.getElementById('Label4').innerText = vQty1 ;" & _
  70.             "document.getElementById('Label5').innerText = vQty2 ; " & _
  71.             "document.getElementById('Label6').innerText = vQty3 ; " & _
  72.             "</script>"
  73.             'end JavaScript
  74.             GetValues = GetValues.Replace("Table1", Table1.ClientID).Replace("Label1", Label1.ClientID)
  75.             GetValues = GetValues.Replace("Label2", Label2.ClientID).Replace("Label3", Label3.ClientID).Replace("Label4", Label4.ClientID)
  76.             GetValues = GetValues.Replace("Label5", Label5.ClientID).Replace("Label6", Label6.ClientID)
  77.             Page.ClientScript.RegisterStartupScript(Me.GetType(), "GetValues", GetValues)
  78.         Else
  79.             Label1.Text = "Table is empty!"
  80.         End If
  81.  
Jan 7 '07 #8

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

Similar topics

2
11230
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a &#xA0; (blank). I need to retieve the text in each cell of that row if the radio button on that row is clicked. Each <TD> has a numeric ID so that I can use it with document.getElementById().innerText method. I use Javascript to assign some...
1
4129
by: Eugfene | last post by:
I have the following function in a html file: function selectEdit(fileID, processCode, processID, fileName,fileDesc) { document.forms.recordID.value = fileID; document.forms.processor.value = processCode; ` document.forms.processorID.value = processID; document.forms.interfaceFileName.value = fileName; document.forms.fileNameDesc.value=fileDesc; document.forms.submit();
5
2439
by: ggk517 | last post by:
We are trying to develop an Engineering application using PHP, Javascript with Informix as the back-end. Is it possible to retrieve data using Javascript but by accessing the Database. Say somebody enters part_no, than using Javascript is it possible to connect to the part master and retrieve the division and desc information? I am not allowed to use the PHP because this will require the user to insert the part number on the first...
13
2203
by: no.mail.pls | last post by:
Hiya, How do i retreive fields with similar values from 2 tables? I tried to use (1) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like '%p.name%'"; but it retrieves nothing at all. (2) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like p.name";
11
1935
by: Patricio | last post by:
Hello all I open a PopUp that receives two variables (values). This PopUp must return a value as well. How can I retrieve this value? Thanks a lot.
3
1501
by: bazubwabo | last post by:
hi everybody , could u please help me to find out the error on my asp codes,i can't retrieve the inserted values. Here is below the code: <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% connect_string = "Driver={SQL Server};Server=NLROSDBCL9-10;Database=Scanners;Uid=scanners;Pwd=bertrand;"
2
5924
by: phpachu | last post by:
Hi, I hav created a group of textboxes using a loop and its names are unique and names are assigned using variable ( like <input type=text name=$name1>). Then How can i retrieve the values in that textboxes after a button click with in a loop. Really I cant move from here. Can anyone plz help me.. Part of the my code is given below <html> <table> <?php // i hav an array "assocArray" and its count is "countarray" for($i = 1; $i <...
4
7558
by: Evanescent | last post by:
Hi Guys, I am trying to create a form which allows the users to retrieve records based on the values entered or chosen in the various combo boxes and textboxes, such as the customer's name, invoice number, service number and installation site. The users do not have to enter all the information for the search. Currently, for the Customer's Name Combo Box, the values are retrieved from the Information Table and when a user selects a name, the...
4
1882
by: Archanak | last post by:
Hi, I have table like this: select * from sampletest; | title | description ...
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7371
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.