473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Learning JS, trying to figure out createelement

Hi there,

I haven't done a lot of Javascript programming, so I'm not having great
luck debugging something I put together, and I was hoping someone here
could give it a quick lookover and maybe see the obvious mistake I'm
making.

My challenge is, I've been asked to make a change to an internal tool.
It has a screen with a tree control and a series of buttons at the top.
The buttons at the top control which form is submitted, and are
enabled/disabled based on what type of things are selected in the tree
below.

It currently uses radio buttons, but I've changed the items that are
parents to be checkboxes instead so I can have multiselect. To
maintain compatibillity with the other forms that expect a single
variable (named testclass_id) to be posted, I've made the following
action to be executed on submit:

function dealWithMultiSe lect ()
{
/*This function should parse through all tcl set checkboxes to see if
there are more then one
//selected. If so, it needs to create a new hidden field with the
same name for each so we can
//send multiple instances of testclass_id to fulfillments review.
//Suggested flow, go through all of the checkboxes and see if there
are more then one active.
//In a sub loop, for each found, add a new hidden field.
//We'll modify set_val to make sure that there isn't a combination of
tcs and tcls set*/
num = document.tc.id. length;
for (var i = 0; i < num; i++)
{
if (document.tc.id[i].checked == true && document.tc.id[i].type ==
"checkbox")
{
//if it's not the FIRST tcl selected, then create a new hidden
element named testclass_id
if (TotalTCLSelect ed)
{
var hidden = document.create Element('input' );
hidden.setAttri bute('type','hi dden');
hidden.setAttri bute('value',do cument.tc.id[i].value);
hidden.setAttri bute('name','te stclass_id');
document.getEle mentById(docume nt.appendChild( hidden));
}
TotalTCLSelecte d++;
}
}
}

As you can see, I'm trying to use createelement to add more hidden
variables of the same name (in this case, 'testclass_id') so that I'll
be emulating a multi-select GET on the next screen. The intention is
that it loops through all the inputs in the form and, if the item is
checked AND is of the type 'checkbox', it adds a new hidden input with
the value of that item. End result, it should submit multiple
instances of 'testclass_id'. I increment TotalTCLSelecte d so that it
doesn't start doing that until the second one it encounters on purpose,
that's also to maintain compatibillity with other forms.

Can anyone offer some suggestions? My main theory is that the problem
has to do with one or more of the following:
1. Can I really keep redefining the var hidden in the loop? And does
it get cleared each time?
2. Am I messing up the object inheritance by doing
document.create element instead of something like
document.tc.cre ateelement? (where tc is the name of the form being
parsed)
3. I didn't define TotalTCLSelecte d anywhere. It isn't erroring out,
but could this be silently killing it?
4. Other?

Thanks!

Jul 23 '05 #1
3 1778


Ben Hallert wrote:

var hidden = document.create Element('input' );
hidden.setAttri bute('type','hi dden');
hidden.setAttri bute('value',do cument.tc.id[i].value);
hidden.setAttri bute('name','te stclass_id');
document.getEle mentById(docume nt.appendChild( hidden));


The last line is somehow nonsense, you should call appendChild on the
element you want to append the input to, probably the form element or a
descendant of the form e.g.
document.forms. formName.append Child(hidden);
The document.getEle mentById is not needed at all.
Also consider to set value and defaultValue of the input, otherwise some
browsers will not have the value set for the newly created input:
hidden.value = hidden.defaultV alue =
document.tc.id[i].value;

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Ben Hallert wrote:
[...]
num = document.tc.id. length;
If this is intended to get all the input elements of a form with
id="tc", you should consider getting all the form elements and then
use that:

var ele = document.forms['tc'].elements;

and then you could change:
for (var i = 0; i < num; i++)
{
if (document.tc.id[i].checked == true && document.tc.id[i].type ==
"checkbox")
to

for (var i = 0; i < ele.length; i++) {
if (ele[i].type == "checkbox" && ele[i].checked ) {

If you test if it's checked first, then the second test is more or less
redundant since only a checkbox can be checked. But you also depend on
non-checkboxes returning evaluating to false, which probably should
happen but may not.
{
//if it's not the FIRST tcl selected, then create a new hidden
element named testclass_id
if (TotalTCLSelect ed)
You haven't declared TotalTCLSelecte d. Do so near the start of your
script (before the for loop), and change the if statement:

var TotalTCLSelecte d = 0; // set to zero
...
if (TotalTCLSelect ed == 0) // or <1 or !> 0 whatever
{ ... }
TotalTCLSelecte d++;

If you are not going to use the value of TotalTCLSelecte d, make it a
boolean:

var TotalTCLSelecte d = false;
if (TotalTCLSelect ed == 0)
{ ... }
TotalTCLSelecte d = true;

Of course you may then want to change the name to 'pastFirstTCL' or
similar.
{
var hidden = document.create Element('input' );
hidden.setAttri bute('type','hi dden');
hidden.setAttri bute('value',do cument.tc.id[i].value);
You really need better names for variables, change "hidden" the
variable name to hiddenInput or something otherwise it is very
confusing for anyone trying to maintain your code.

Why not (if you've used my suggestion above and renamed 'hidden')

hiddenInput.val ue = ele[i].value;
hidden.setAttri bute('name','te stclass_id');
This will give all your hidden inputs the same name, which whilst
invalid HTML will likely not cause any problems at the client end
(unless you want to refer to them by id). Consider:

hiddenInput.nam e = 'testclass_id' + i;
document.getEle mentById(docume nt.appendChild( hidden));
}
TotalTCLSelecte d++;
}
I think your appendChild error has been well covered. To make life
much easier, why not include the hidden inputs in the HTML?

[...] 1. Can I really keep redefining the var hidden in the loop? And does
it get cleared each time?
It's OK.
2. Am I messing up the object inheritance by doing
document.create element instead of something like
document.tc.cre ateelement? (where tc is the name of the form being
parsed)
No. You create the element, then add it to a collection.
3. I didn't define TotalTCLSelecte d anywhere. It isn't erroring out,
but could this be silently killing it?
Very likely (it doesn't work for me). Just declare it before using it
in the if statement.
4. Other?


Of course I haven't tested any of the above very much since I only have
your snippet and no HTML. If you have any issues post your modified
code & I'll have another go.

--
Rob
Jul 23 '05 #3
RobG wrote:
[...]
if (TotalTCLSelect ed == 0) // or <1 or !> 0 whatever


That should have been:

if (TotalTCLSelect ed != 0) // or <1 or !> 0 whatever
--
Rob
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1760
by: Ryan Walker | last post by:
Hi, I'm getting started with python and have almost zero programming experience. I'm finding that there are tons of tutorials on the internet -- such as the standard tutorial at python.org -- that tell you all about the language. That is, what are the methods, functions, modules, syntax, punctuation, etc. No problem there! Meanwhile, I'm...
25
5141
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to calculate the totals in each row. when i try however, i receive the error: "Error: 'elements' is null or not an object"
2
42850
by: kie | last post by:
hello, when i create elements and want to assign events to them, i have realised that if the function assigned to that element has no parameters, then the parent node values can be attained. e.g. aTextBox=document.createElement('input'); aTextBox.onchange=calculateOneRow2;
0
1547
by: Henry | last post by:
I am trying to figure out how to create a .NET windows Forms application that also use Microsoft Excel. I need help simply opening and closing the workbooks. Any help you can provide is greatly appreciated. My end goal is to build an app to manipulate data in selected workbooks to reformat it for data input to another application. ...
1
9597
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the...
2
9120
by: Thirsty Traveler | last post by:
How would I dynamcially create an XML document of the following form: <ReceiveTSSCallBack xmlns="http://LandAm.EAI.Mainframe.TSR"> <TSSCallBack xmlns="http://LandAm.EAI.Mainframe.TSR.Schemas.TSSCallback"> <orderNo xmlns="">string</orderNo> <customerId xmlns="">string</customerId> <taxingAuthorityList xmlns=""> <string>string</string>...
3
3978
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table with a createElement function to add additional entries. The table has the first row of input text boxes already in it. You have to click a button to...
4
1388
tpgames
by: tpgames | last post by:
The original game worked for 15 tile sliding game. I wanted a 24 tile game. What's wrong? var tiles = ,,,,]; var grid = ,,,,]; var game = document.createElement("div"); game.className = "game"; game.style.position = "absolute"; // give tiles something to "stick" to game.style.width = width + "px"; game.style.height = height + "px";
0
7479
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...
0
7411
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...
0
7669
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. ...
0
7926
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...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
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...
0
3468
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...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
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

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.