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 !
|
|
0
|
|
|
|
Reply
|
dodgeyb
|
9/21/2007 10:32:15 AM |
|
On Sep 21, 5:32 am, dodgeyb <ch...@ctlsoft.com> wrote:
> 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
|
|
0
|
|
|
|
Reply
|
Deft
|
9/21/2007 1:00:00 PM
|
|
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!
|
|
0
|
|
|
|
Reply
|
Deft
|
9/21/2007 3:35:44 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
dodgeyb
|
9/22/2007 10:30:25 AM
|
|
Deft.Jab@gmail.com wrote:
> 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.
|
|
0
|
|
|
|
Reply
|
Richard
|
9/22/2007 9:01:22 PM
|
|
Deft.Jab@gmail.com said the following on 9/21/2007 11:35 AM:
> 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
I don't believe you.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
0
|
|
|
|
Reply
|
Randy
|
9/23/2007 4:42:07 AM
|
|
First some help:
DodgeyB -
var ele = document.getElementsByTagName('INPUT');
if(ele != null)
for(i=0;i<ele.length;i++)
{
if(!ele[i].disabled)
{
o = eval(self.opener.document.Form1.txtResult);
o.value = ele[i].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[i].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.
|
|
0
|
|
|
|
Reply
|
Deft
|
9/24/2007 4:03:01 PM
|
|
Deft.Jab@gmail.com said the following on 9/24/2007 12:03 PM:
> First some help:
The least you could do is qualify your "help" as doing more harm than
good with the bad practices in it.
> DodgeyB -
>
> var ele = document.getElementsByTagName('INPUT');
And if the browser doesn't support getElementsByTagName?
> if(ele != null)
> for(i=0;i<ele.length;i++)
> {
> if(!ele[i].disabled)
> {
> o = eval(self.opener.document.Form1.txtResult);
eval? Can you explain what you think the call to eval accomplishes there?
> o.value = ele[i].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.
The least you could do is learn what the "right direction" would be
before trying to point someone in that direction.
<snip>
> 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.
Then why did you duplicate it in your second code posted?
> "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.
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 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.
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.
> Bottom Line:
Bottom Line:
Don't post garbage here and you won't have it pointed out to you.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
0
|
|
|
|
Reply
|
Randy
|
9/24/2007 4:52:01 PM
|
|
Deft.Jab@gmail.com wrote:
> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
|
|
0
|
|
|
|
Reply
|
Thomas
|
9/24/2007 5:22:06 PM
|
|
> The least you could do is qualify your "help" as doing more harm than
> good with the bad practices in it.
>
> > DodgeyB -
>
> > var ele = document.getElementsByTagName('INPUT');
>
> 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[i].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?
> > Thanks for pointing out the global variable, that is good input. I
> > will revise my ways of using it in the future.
>
> 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(i=0;i<ele.length;i++)
> 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.
|
|
0
|
|
|
|
Reply
|
Deft
|
9/24/2007 8:20:17 PM
|
|
Deft.Jab@gmail.com said the following on 9/24/2007 4:20 PM:
>> The least you could do is qualify your "help" as doing more harm than
>> good with the bad practices in it.
>>
>>> DodgeyB -
>>> var ele = document.getElementsByTagName('INPUT');
>> 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.
Any browser that would fail this test:
if(document.getElementsByTagName &&
document.getElementsByTagName('INPUT'))
At the minimum.
> IE6, IE7, FF(latest), Safari(latest) all support this method,
So you tested it in four browsers and proclaimed it to "work in all
browsers" and I still don't believe that claim simply because I know better.
> how many modern browsers don't? Honestly what would you do there?
You are still under the impression that I am obligated to provide
answers to peoples questions. I am not. This isn't a help desk, it is a
discussion group. We discuss things. If someone gets an answer to a
question, then great. If they don't, that is fine too.
Just to appease you though, any solution I come up with won't involve
getElementsByTagName and whatever solution I came up with would use the
proper feature detection.
>>> if(ele != null)
>>> for(i=0;i<ele.length;i++)
>>> {
>>> if(!ele[i].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.
Then you should point out the flaw in it. The code is in *your* code. It
doesn't make a hill of beans who else wrote it, you posted it as your
own and it is bad coding.
> 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.
So, you want to provide "help" but when the flaws in that help are
pointed out you want to call people "pricks" for pointing it out? Wow.
>> 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?
If you think that was bashing you, you don't know what a real bashing is.
>>> Thanks for pointing out the global variable, that is good input. I
>>> will revise my ways of using it in the future.
>> Then why did you duplicate it in your second code posted?
>
> It was a copy. I did make a mention of it.
That's nice. Search the archives for that phrase and you can find a URL
to look up what it means.
>> 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.
Nonsense. Your claim was, and I quote, "That will work in all browsers"
to which I replied "I don't believe you". So, you have not even posted a
"working loop" unless you define "working" to be the limited set of
browsers you tested it on.
> I saw an unanswered post, and wanted to move the thread along.
What a humanitarian you are.
> 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.
You are still under the misguided impression that this is a freaking
help desk when it isn't.
>> 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.
And just think. If you had done that, none of this would be written.
Simple isn't it?
<snipped the idiotic moronic mixing of my post and Thomas'>
> Once again, MY NON-STANDARD/INEFFICIENT/"GARBAGE" code is better than
> what the original poster had, nothing.
That doesn't keep it from being garbage.
> I won't look back at this thread, so reply/bash as you will.
If only we were that lucky.
> I just find it very unhelpful to gripe at code and not post any suggestions.
And I find it "very unhelpful" for people to post garbage and claim it
is "better than nothing" and then have an OP come back in a few months
(after you have vanished) and have to explain to them why your advice
was garbage.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
0
|
|
|
|
Reply
|
Randy
|
9/24/2007 9:30:50 PM
|
|
|
10 Replies
60 Views
(page loaded in 0.174 seconds)
|