help looping controls on form

D

dodgeyb

Hi there, newbie here, trying to write some clientside javascript to
find which textarea is not disabled and pass text back to opener page.

I have potentially many textareas which get created on the fly (by
server) on the form, and only ONE of them will be enabled. I need to
find that one and send that text back to the opener window, so I need
to loop rather than go through by name.

I know I can send the data back like :

o = eval(self.opener.document.Form1.txtResult);
o.value = self.document.Form1.ctlName.value;

Not sure how to start looping controls...
Any guidance much apprec'd !
 
D

Deft.Jab

Hi there, newbie here, trying to write some clientside javascript to
find which textarea is not disabled and pass text back to opener page.

I have potentially many textareas which get created on the fly (by
server) on the form, and only ONE of them will be enabled. I need to
find that one and send that text back to the opener window, so I need
to loop rather than go through by name.

I know I can send the data back like :

o = eval(self.opener.document.Form1.txtResult);
o.value = self.document.Form1.ctlName.value;

Not sure how to start looping controls...
Any guidance much apprec'd !

I'm no expert but I would probably use something like:

var ele = document.getElementsByTagName('INPUT');
if(ele != null)
for(i=0;i < ele.length;i++) //length is the number of controls it
found
{
//Check for text or w/e needs to happen
}

This will get you a collection of all the input controls on the page.
Then it's a simple loop =) Hope this helps. You can do this for DIVs
Tables etc. If you want to loop through ALL controls you can replace
INPUT with a *.

- Jab
 
D

Deft.Jab

Hmm I tried to post earlier...sorry if it posts twice.

I'm no expert but this is my solution.

var ele = document.getElementsByTagName('INPUT');
if(ele != null)
for(i=0;i<ele.length;i++)
{
//Do whatever you need to do.
}

That will work in all browsers, if you need to get all elements and
not just input elements you can replace the INPUT with a *. You can
use this for any type, DIVS, Tables etc. ;) Enjoy and hope it helps!
 
D

dodgeyb

that works great thanks! DO you know how I can tell if a control is
disabled or not ? I'm looping though textareas if it makes a
difference.

TIA !
Chris
 
R

Richard Cornford

Hmm I tried to post earlier...sorry if it posts twice.

I'm no expert but this is my solution.

Solution to what exactly?
var ele = document.getElementsByTagName('INPUT');
if(ele != null)

As the - getElementsByTagName - method is required to always return a
NodeList interface implementing object there is little point in seeing
how that object compares will null, a NodeList is never going to be
equal to null.
for(i=0;i<ele.length;i++)

It is always a bad idea to use (what effectively is) a global variable
as a loop counter. In any particular instance it may not matter, but
doing so habitually will eventually cause problems, as sooner or later
the body of a loop will call a function that also uses that global
variable as a loop counter.
{
//Do whatever you need to do.

You don't think demonstrating how to reference an individual element
within the NodeList would be a good idea here?
}

That will work in all browsers,

It certainly will not "work in all browsers", it is employing a method
defined in the W3C Core DOM level 1, so it will 'work' in script enabled
W3C DOM level 1+ compliant browsers.

Except that form controls may also be SELECT, TEXTAREA and BUTTON
elements (indeed the questions asked seems directed towards TEXTAREAs).
if you need to get all elements and not just input
elements you can replace the INPUT with a *.
<snip>

And that 'wildcard' was introduced in DOM level 2, so that is slightly
fewer browsers where it will 'work'.

Richard.
 
D

Deft.Jab

First some help:
DodgeyB -

var ele = document.getElementsByTagName('INPUT');
if(ele != null)
for(i=0;i<ele.length;i++)
{
if(!ele.disabled)
{
o = eval(self.opener.document.Form1.txtResult);
o.value = ele.value;
}
}
The ele is a node list of elements returned by the method. Using the
loop you have an index for each element it returns. So ele[0] is the
first element in the returned nodelist this continues on. This is the
quick and dirty explanation, I'm no teacher I'm just trying to point
you in the right direction. if(!ele.disabled) is checking each
element as the loop goes on and testing the disabled field, and if it
is it's false(means the control is enabled) it sets your opener code
your posted earlier. This will need testing and tweaking I'm sure, I
haven't tested this at all. I will say there are better ways to do
some of these things, but I hope this helps you out. As mentioned
above you can declare the i variable inside the function to avoid
conflicts with other pieces of code.

On to the pricks: Randy, Richard
Thanks for pointing out the global variable, that is good input. I
will revise my ways of using it in the future.

"You don't think demonstrating how to reference an individual element
within the NodeList would be a good idea here? "
Don't YOU think demonstrating how to reference an individual element
within the NodeList would be a good idea here? Stop being a prick and
instead of bashing me for not looking up how to do it and answer it
yourself. Re-do my example and show him the proper way instead of
being a jackoff.

"I don't believe you."
Gratz on accomplishing nothing, you are an expert with your FAQ and
Best Practices links but yet you don't offer any help. Get over
yourself and help out someone. Hypocrit.

Bottom Line:
Don't go out of your way to point out my problems without at least
showing me a better way, explaining why I should avoid the things I
showed. I mean really, are you that upset with your life you go
bashing a guy on a public discussion about javascript? Seriously, Grow
up, I was trying to help. My ways are not the best but with noone
else helping my way is better than nothing.
 
T

Thomas 'PointedEars' Lahn

var ele = document.getElementsByTagName('INPUT');

Doing proper feature tests before calling a DOM method would be wise.
if(ele != null)

if (ele)

is sufficient, more efficient and even less error-prone as it covers all
possible false-values.
for(i=0;i<ele.length;i++)

for (var i = 0, len = ele.length; i < len; i++)

is more efficient. As in this case order appears to be insignificant,

for (var i = ele.length; i--;)

is even more efficient.

I won't comment on the rest of your posting; it isn't worth it.


PointedEars
 
D

Deft.Jab

The least you could do is qualify your "help" as doing more harm than
good with the bad practices in it.



And if the browser doesn't support getElementsByTagName?

I already mentioned I was no expert, but it works in all major
browsers not in quirks mode. Once again you failed to mention a
better alternative to HELP to original poster. At least explain which
browsers/combinations don't support the method. IE6, IE7, FF(latest),
Safari(latest) all support this method, how many modern browsers
don't? Honestly what would you do there?
if(ele != null)
for(i=0;i<ele.length;i++)
{
if(!ele.disabled)
{
o = eval(self.opener.document.Form1.txtResult);


eval? Can you explain what you think the call to eval accomplishes there?


If you read the entire post you would note that this was his code
snippet put in. That line is a direct copy, I am simply trying to
show what "could" be done inside the loop based on what I know. I'm
didn't post to code all his javascript for him. The thread is help
looping controls on a form.
The least you could do is learn what the "right direction" would be
before trying to point someone in that direction.

Once again bashing the little help given out but without correction.
What would you do?
Then why did you duplicate it in your second code posted?

It was a copy. I did make a mention of it.
So, you think you should just post some garbage and hope someone else
will correct your mistakes rather than you asking for help with code
that you didn't have time to test for yourself?

I posted a working loop, I never stated it met all standards or best
practice measures. I saw an unanswered post, and wanted to move the
thread along. My "garbage" is better than what you posted, nothing.
Why don't you reply with a working code example instead of critiquing
code by the only person helping out the poster? Or better yet, say
that would work but...with suggestions on improving or replacing parts
of the example with reasons. I just don't understand why you bother
posting without some sort of help to the original poster.
I will be sure to add "prick" to the already hundreds of things I have
been called. Telling you "I don't believe you" was a nicer way, to me,
than saying "You are an idiot spewing pure unadulterated bullshit". And
that is what your claim of "This will work in all browsers" is, pure
unadulterated bullshit. Perhaps you should take some time and read those
two documents and get a hint of what caused me to say what I said.

I said I was no expert, which means I know my code wasn't perfect. I
should have left ALL out of the statement. Just IE6 IE7 FF(latest) and
Safari(latest, beta) in standards compliant mode.
Doing proper feature tests before calling a DOM method would be wise.

for (var i = 0, len = ele.length; i < len; i++)
if(ele != null)

if (ele)

is sufficient, more efficient and even less error-prone as it covers
all
possible false-values.
is more efficient. As in this case order appears to be insignificant,
for (var i = ele.length; i--;)
is even more efficient.

My code is by far not production level code. Pointed Ears, These are
the kinds of posts that ARE helpful. Now when someone sees this thread
they could see that they can change the loop with a more efficient
loop. That the (ele) would be a better fit and a piece of code to test
the feature would have been even better. That's what these discussion
groups are for, discussion and being able to help each other's code
grow. Seriously, I never mentioned anything about my code being right
or perfect did I? I come back to see if it worked out for him or not
or if anyone else helped him if my solution had a problem but come
back to smart ass 'I don't believe you' with NO ADVICE on how to fix
it for the original poster. I could care less if you bash my code but
at least offer some sort of solution as an alternative.

Once again, MY NON-STANDARD/INEFFICIENT/"GARBAGE" code is better than
what the original poster had, nothing.

I won't look back at this thread, so reply/bash as you will. I just
find it very unhelpful to gripe at code and not post any suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top