Script help

B

Bob Bedford

after Evertjan advice ;), I'm trying to cut down a complex task in many
easier ones.

Here is the code I'm trying to get to work:

function CheckGroup(X){
code = X.value;
alert(code); //this is OK !!!!

obj = document.getElementById('option[]');
alert('object length '+obj.length);
for ( index = 0; index < obj.length; index++ )
{
testcode = obj[index].value;
if(code == testcode)
alert('found...');
}
}

then I print several input type like this:
//generated by PHP...$newvalue are new values at every checkbox (concat in
PHP is ".")
<input type="checkbox" name="option[]" value=".$newvalue."
onclick="CheckGroup(this)");>

Every checkbox is part of the option array (look at the name). In the
CheckGroup function, I try to retrieve all $newvalue from the option[]
array. So I try to retrieve every single item in the option[] array, but
unfortunately, it doesn't work.
alert(obj.length); show and undefined value.

How can I retrieve such items values ???
 
E

Evertjan.

Bob Bedford wrote on 01 jul 2004 in comp.lang.javascript:
after Evertjan advice ;), I'm trying to cut down a complex task in
many easier ones.

Here is the code I'm trying to get to work:

function CheckGroup(X){
code = X.value;
alert(code); //this is OK !!!!

obj = document.getElementById('option[]');
alert('object length '+obj.length);
for ( index = 0; index < obj.length; index++ )
{
testcode = obj[index].value;
if(code == testcode)
alert('found...');
}
}

then I print several input type like this:
//generated by PHP...$newvalue are new values at every checkbox
(concat in PHP is ".")
<input type="checkbox" name="option[]" value=".$newvalue."

name="option[]"

1 This is not a HTML legal name.
2 this is not an ID, so getElementById should not find it [exept that som
browsers might have a bug.
3 id ="option[]" would aldso be illegal HTML.
4 you cannot make a javascript array like this

onclick="CheckGroup(this)");>

the ); is illegal
Every checkbox is part of the option array (look at the name). In the
CheckGroup function, I try to retrieve all $newvalue from the option[]
array. So I try to retrieve every single item in the option[] array,
but unfortunately, it doesn't work.
alert(obj.length); show and undefined value.

How can I retrieve such items values ???

I can only exchange PHP [because of my lack of knowledge of PHP]
by ASP(!!!) and you would get something like this:

<% for n= 1 to 25 %>
<input type="checkbox"
name="<%=option(n)%>"
id="<%=option(n)%>"
onclick="CheckGroup(this);"
value="<%=newvalue(n)%>">
<br>
<% next %>

And the clientside javascript can do:

function CheckGroup(X){
id = X.value;
alert(id); //this is OK !!!
}

and

obj = document.getElementById('<%=option(7)%>');
alert(obj.value)

==============

Bob, perhaps we will get somewhere.
 
R

Randy Webb

Evertjan. said:
<input type="checkbox" name="option[]" value=".$newvalue."


name="option[]"

1 This is not a HTML legal name.

Sure it is.
2 this is not an ID, so getElementById should not find it [exept that som
browsers might have a bug.
3 id ="option[]" would aldso be illegal HTML.

That is true, but as a name attribute, its perfectly legal and valid.

http://www.jibbering.com/faq/#FAQ4_25

<quote>
These characters are illegal within ID attributes in the standard
(x)HTML doctypes and javascript Identifiers
</quote>

Illegal in ID's, perfectly valid in NAMES.
 
M

Martin Honnen

Bob Bedford wrote:

Here is the code I'm trying to get to work:

function CheckGroup(X){
code = X.value;
alert(code); //this is OK !!!!

obj = document.getElementById('option[]');
alert('object length '+obj.length);
for ( index = 0; index < obj.length; index++ )
{
testcode = obj[index].value;
if(code == testcode)
alert('found...');
}
}

then I print several input type like this:
//generated by PHP...$newvalue are new values at every checkbox (concat in
PHP is ".")
<input type="checkbox" name="option[]" value=".$newvalue."
onclick="CheckGroup(this)");>

Don't post your PHP here, we deal with HTML and JavaScript so use
view-source in your browser so that you can post HTML/JavaScript as the
browser sees it and not any server side scripting.

Now to your code, you pass the <input> element object to the CheckGroup
function which is fine but then you try
document.getElementById('option[]');
which is nonsense as you do not have any element with that id, what you
have are elements with that name. What you are looking for is
var inputs = X.form.elements[X.name];
This will give you a collection of all the controls in the form that
have the same name as the input you pass in as X.

As long as you are in the realms of form scripting the JavaScript 1.3
client-side documentation applies, I suggest you have a look at
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/
which deals with that
 
B

Bob Bedford

Evertjan. said:
Bob Bedford wrote on 01 jul 2004 in comp.lang.javascript:
after Evertjan advice ;), I'm trying to cut down a complex task in
many easier ones.

Here is the code I'm trying to get to work:

function CheckGroup(X){
code = X.value;
alert(code); //this is OK !!!!

obj = document.getElementById('option[]');
alert('object length '+obj.length);
for ( index = 0; index < obj.length; index++ )
{
testcode = obj[index].value;
if(code == testcode)
alert('found...');
}
}

then I print several input type like this:
//generated by PHP...$newvalue are new values at every checkbox
(concat in PHP is ".")
<input type="checkbox" name="option[]" value=".$newvalue."

name="option[]"

1 This is not a HTML legal name.
2 this is not an ID, so getElementById should not find it [exept that som
browsers might have a bug.
3 id ="option[]" would aldso be illegal HTML.
4 you cannot make a javascript array like this

onclick="CheckGroup(this)");>

the ); is illegal
Every checkbox is part of the option array (look at the name). In the
CheckGroup function, I try to retrieve all $newvalue from the option[]
array. So I try to retrieve every single item in the option[] array,
but unfortunately, it doesn't work.
alert(obj.length); show and undefined value.

How can I retrieve such items values ???

I can only exchange PHP [because of my lack of knowledge of PHP]
by ASP(!!!) and you would get something like this:

<% for n= 1 to 25 %>
<input type="checkbox"
name="<%=option(n)%>"
id="<%=option(n)%>"
onclick="CheckGroup(this);"
value="<%=newvalue(n)%>">
<br>
<% next %>

And the clientside javascript can do:

function CheckGroup(X){
id = X.value;
alert(id); //this is OK !!!
}

and

obj = document.getElementById('<%=option(7)%>');
alert(obj.value)

==============

Bob, perhaps we will get somewhere.

Finally, with your help, I got it to work:
Javascript:
function CheckGroup(X){
code = X.value;
alert(code);
obj = document.SubmitForm.option;
for ( index = 0; index < obj.length; index++ ){
alert('object values '+obj[index].value);
}
}

HMTL code:
....
<input type="checkbox" name="option" value="1422862"
onclick="CheckGroup(this)">
<input type="checkbox" name="option" value="1422863"
onclick="CheckGroup(this)">
....

Thanks for your help !!!

Now I will try to store a list of exclusion with this object. If somebody
know how to do so, please let me know.

Regards
 
B

Bob Bedford

Hi Evertjan,

Finally, I've been able to do what I wanted in the original post.

Thanks for your help !

(if you are interested, let me know....)

Best regards.

BoB
 
T

Thomas 'PointedEars' Lahn

Randy said:
Evertjan. said:
2 this is not an ID, so getElementById should not find it [exept
that som browsers might have a bug.
3 id ="option[]" would aldso be illegal HTML.

That is true, but as a name attribute, its perfectly legal and valid.

http://www.jibbering.com/faq/#FAQ4_25

<quote>
These characters are illegal within ID attributes in the standard
(x)HTML doctypes and javascript Identifiers
</quote>

I think this FAQ entry has created enough
misunderstandings to be rewritten.
Illegal in ID's, perfectly valid in NAMES.

Not *all* NAMEs because the "name" attributes you
are referring to are of type CDATA:

<http://www.w3.org/TR/html4/types.html#h-6.2>
<http://www.w3.org/TR/html4/index/attributes.html>


PointedEars
 
R

Randy Webb

Thomas said:
Randy said:
Evertjan. said:
2 this is not an ID, so getElementById should not find it [exept
that som browsers might have a bug.
3 id ="option[]" would aldso be illegal HTML.

That is true, but as a name attribute, its perfectly legal and valid.

http://www.jibbering.com/faq/#FAQ4_25

<quote>
These characters are illegal within ID attributes in the standard
(x)HTML doctypes and javascript Identifiers
</quote>


I think this FAQ entry has created enough
misunderstandings to be rewritten.

But it is factually correct as written, and the current wording was
chosen for a very explicit reason.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top