473,769 Members | 4,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

appending to variable using document.getEle mentById

127 New Member
Hi,

I have a JS function which includes the following lines:

Expand|Select|Wrap|Line Numbers
  1. var table = document.getElementById("table1");
  2. var cells = table.getElementsByTagName("div");
  3. for (i = 0; i < cells.length; i++) {
  4. checkedCels = cells[i].id;
  5. var checkboxes = cells[i].getElementsByTagName("input");
  6.  
Is it possible to set "var table" from more than 1 table (i.e., "table2")? I tried to include the line "var table += document.getEle mentById("table 2");", but it didn't work.

Thanks for any suggestions!
Feb 16 '08 #1
45 9567
wyatt
6 New Member
The following should do what you're looking for:

Expand|Select|Wrap|Line Numbers
  1. var table = document.getElementById("table1");
  2. var cells = table.getElementsByTagName("div");
  3.  
  4. table = document.getElementById("table2");
  5. var cells2 = table.getElementsByTagName("div");
  6.  
  7. var cells = cells.concat(cells2);
  8.  
  9. for (i = 0; i < cells.length; i++) {
  10. checkedCels = cells[i].id;
  11. var checkboxes = cells[i].getElementsByTagName("input");
  12.  
Feb 18 '08 #2
phub11
127 New Member
Thanks for the response; however, that doesn't seem to handle the variables the way I'd like. Below is more along the lines of what I *think* I need.

If possible, could someone please fix the syntax? I can't seem to get the assignment checks to work.

Thanks!

Expand|Select|Wrap|Line Numbers
  1. function createOrder() {
  2. var checkedData = "";
  3. ////var table = document.getElementById("table1");
  4. ////var cells = table.getElementsByTagName("div");
  5. var table1 = document.getElementById("table1");
  6. var table10 = document.getElementById("table10");
  7. if (table1.value != 'undefined') {
  8. var cells = table1.getElementsByTagName("div");
  9. } else if (table10.value != 'undefined') {
  10. var cells = table10.getElementsByTagName("div");
  11.  
Feb 20 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
So you want table1 OR table10, not table1 AND table10? Note that a table doesn't have a value.
Feb 20 '08 #4
phub11
127 New Member
Hi!

I'd prefer to have table1 AND table 10 (sorry about the numbering - it's on a log scale!). Any ideas?

Thanks!
Feb 20 '08 #5
phub11
127 New Member
Hi!

I'd prefer to have table1 AND table 10 (sorry about the numbering - it's on a log scale for some reason!). I thought OR would work as it loops each time and appends to an array.

Any ideas?

Expand|Select|Wrap|Line Numbers
  1. function createOrder() {
  2. var checkedData = "";
  3. ////var table = document.getElementById("table1");
  4. ////var cells = table.getElementsByTagName("div");
  5. var table1 = document.getElementById("table1");
  6. var table10 = document.getElementById("table10");
  7. if (table1.value != 'undefined') {
  8. var cells = table1.getElementsByTagName("div");
  9. } else if (table10.value != 'undefined') {
  10. var cells = table10.getElementsByTagName("div");
  11. }
  12. for (i = 0; i < cells.length; i++) {
  13. checkedCels = cells[i].id;
  14. var checkedNams = "";
  15. var checkedVals = "";
  16. var checkedScreen = "";
  17. var checkboxes = cells[i].getElementsByTagName("input");
  18. for (j = 0; j < checkboxes.length; j++) {
  19. if (checkboxes[j].checked) {
  20. checkedNams += checkboxes[j].name;
  21. }
  22. checkedVals = checkboxes[j].value;
  23. checkedScreen = checkboxes[0].value;
  24. //MIGHT NEED TO POP OFF FIRST ELEMENT IN checkboxes[].value TO REMOVE screen[]
  25. }
  26. //if ( checkedVals != "" ){
  27. checkedData = checkedData.concat(checkedCels + checkedScreen + checkedNams + checkedVals)
  28. //        }
  29. }
  30. document.form1.sendData.value = checkedData;
  31. }
  32.  
Thanks!
Feb 20 '08 #6
acoder
16,027 Recognized Expert Moderator MVP
I'd prefer to have table1 AND table 10 (sorry about the numbering - it's on a log scale for some reason!). I thought OR would work as it loops each time and appends to an array.
If you want both, why wouldn't the code posted by wyatt not work? It takes the divs from table10 and concatenates them to the divs from table1.

What are you trying to test in the following lines:
Expand|Select|Wrap|Line Numbers
  1. if (table1.value != 'undefined') {
  2. var cells = table1.getElementsByTagName("div");
  3. } else if (table10.value != 'undefined') {
  4. var cells = table10.getElementsByTagName("div");
  5. }
  6.  
A table doesn't have a value attribute.
Feb 20 '08 #7
phub11
127 New Member
Hi! I just retried wyatts code, and the array "checkedDat a" is blank - even if I include a string. Still, I appreciate wyatt's suggestion.

The code you referenced was an attempt to avoid using "concat". I thought that it would set "cells" using the original code which works, depending on which table I had dragged the draggable to.

Here is the code with wyatts suggestion which does not appear to work. More suggestions please!:

Expand|Select|Wrap|Line Numbers
  1. function createOrder() {
  2. var checkedData = "";
  3. //THIS WORKS WITH 1 TABLE//var table = document.getElementById("table1");
  4. //THIS WORKS WITH 1 TABLE//var cells = table.getElementsByTagName("div");
  5. //var table1 = document.getElementById("table1");
  6. //var table10 = document.getElementById("table10");
  7. //if (table1.value != 'undefined') {
  8. //var cells = table1.getElementsByTagName("div");
  9. //} else if (table10.value != 'undefined') {
  10. //var cells = table10.getElementsByTagName("div");
  11. //}
  12.  
  13. var table = document.getElementById("table1");
  14. var cells = table.getElementsByTagName("div");
  15. table = document.getElementById("table10");
  16. var cells2 = table.getElementsByTagName("div");
  17. var cells = cells.concat(cells2);
  18.  
  19. for (i = 0; i < cells.length; i++) {
  20. checkedCels = cells[i].id;
  21. var checkedNams = "";
  22. var checkedVals = "";
  23. var checkedScreen = "";
  24. var checkboxes = cells[i].getElementsByTagName("input");
  25. for (j = 0; j < checkboxes.length; j++) {
  26. if (checkboxes[j].checked) {
  27. checkedNams += checkboxes[j].name;
  28. }
  29. checkedVals = checkboxes[j].value;
  30. checkedScreen = checkboxes[0].value;
  31. //MIGHT NEED TO POP OFF FIRST ELEMENT IN checkboxes[].value TO REMOVE screen[]
  32. }
  33. //if ( checkedVals != "" ){
  34. checkedData = checkedData.concat(checkedCels + checkedScreen + checkedNams + checkedVals")
  35. //        }
  36. }
  37. document.form1.sendData.value = checkedData;
  38. }
  39.  
Feb 20 '08 #8
acoder
16,027 Recognized Expert Moderator MVP
Hi! I just retried wyatts code, and the array "checkedDat a" is blank - even if I include a string.
And it's not blank when using one table?

The code you referenced was an attempt to avoid using "concat". I thought that it would set "cells" using the original code which works, depending on which table I had dragged the draggable to.
Another alternative is to get the elements from the parent of the tables, thus avoiding concatenation.
Feb 21 '08 #9
phub11
127 New Member
The code doesn't work with just 1 table either, even if I do:

Expand|Select|Wrap|Line Numbers
  1. var cells2 = "";
  2.  
Could you please show me example code of how this alternative method works?

Thanks!
Feb 21 '08 #10

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

Similar topics

3
5887
by: Byron | last post by:
Hi, Javascript confuses me, so I usually limit myself to Dreamweaver's built-in scripts for stuff like imageswaps. But this time I'm trying to write something very simple myself. I do most of my stuff in ASP and PHP so I'm familiar with server-side programming; for some reason JavaScript syntax trips me up. I want to assign a value to a variable according to an onclick event, and then run an if...then on the variable, and according to...
6
10156
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and have the function update the target ID with the URL. There is a bit more to it then that, but that is the basics. my difficulty comes when I try to assign a variable to: "document.getElementById('-> var gose here <-').innerHTML"
20
7000
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
6
9391
by: paul | last post by:
HI! How do we send a variable from an Iframe page back to its parent? I have a script that calculates the iframe's window size but I need to know how to send that value back to its parent so I can use it there. Thanks in advance :) Paul
11
2994
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application as fast as possible. I have about 10-20 id tags that need to be accessed and modified from time to time. Would the jvm perform slowly if I stored all of the dom node strings "document.node.child...." into a huge js array?
7
9508
by: Daz | last post by:
Hi everyone! Is it possible to take a line of text like so: <tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr> And append it to a DOM node such as this: var nodeToAppendTo = document.getElementById('tbody');
1
4531
by: X l e c t r i c | last post by:
webtv versions prior to 2.9 do not recognize document.getElementById, so we have to use a conditional that includes document.all. I've been trying to use a variable for document.all and document.getElementById to simplify my functions. It works for the latest versions of Explorer, Opera, and webtv. It does not work for the latest versions of Firefox, Netscape, and SeaMonkey. Here are two examples of what I'm trying to do. It's the "DOM...
7
1822
by: joecap5 | last post by:
I have a main window from which I want to open a separate side menu window. I then want to create a list of items on that side menu by clicking on the item names in the main window. So far I am able to click on "Open Menu" and have the side menu open. I can then click on Items and have them added to the side menu using the innerHTML method or the node method. The trouble comes in when I try to have the side menu opened automatically...
1
1211
by: irixdude | last post by:
I am trying to create a script to enter the values of an array into a dynamically generated table 3 columns wide. I have a counter for the row # that I am using to name/id the row TR node so I an attach 3 cells (3 columns) before attaching the full row to the table. I'm not sure where the problem is, but in its current form my script doesn't even generate the first node before stopping with an error. Please let me know what I'm doing...
0
10225
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
10053
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...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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
5312
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...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.