Forum Moderators: open

Message Too Old, No Replies

getElementsByTagName

not working well

         

mcibor

10:38 am on Jun 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have many selects populated by database with php:
<select name="sort[]">....<select name="sort[]">...

What I want is to get to each of them. i know how many there are and therefore I thought that this'll do:

frm = document.getElementsByTagName("form");
sel = document.getElementsByTagName("select");

function my {
variable = frm[0].sel[0].value;
//some operation ion variable
frm[0].sel[0].value = variable;//and similar stuff for other selects
}

However Mozilla gives me an error: frm[0].sel has no properties.
Where did I make a mistake?
Best regards
Michal Cibor

RonPK

1:16 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may help to declare frm and sel as global variables:
var frm = ...
var sel = ...
Otherwise they may not be available within the scope of a function.

Also I'd prefer to use something like this to loop through the forms:

var frms = document.getElementsByTagname("form"); 
function myfunction() {
var sels;
for (var i=0; i< frms.length; i++) {
sels = frms[i].getElementsByTagName("select");
// loop through the selects in a similar manner
}
}

Rambo Tribble

1:20 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe RonPK has the solution with the looping constuct. Doing as you have, you have two separate and unrelated arrays.

mcibor

1:42 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



so you mean that sel[0] is really document.form.select? That explains things a bit.
Thanks a lot!

Michal Cibor

Rambo Tribble

2:18 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, sel[0] isn't exactly attached to a single form in this reference. sel[0] is simply the first select to appear on the page, regardless of how many forms are declared. The value returned by getElementsByTagName('select') is just an array of selects, with no particular reference to anything but the order in which they appear on the page.

mcibor

8:14 pm on Jun 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It didn't help.
This is the version:
var frm = document.getElementsByTagName("form");
var sel = frm[0].getElementsByTagName("select");

function js_saveTxt()
{
var f = sel[0].value + "%2" +
sel[1].value + "%2";
}

And the error is:
Error: frm[0] has no properties
In line 2

What to do? I'm no expert in javascript. I just want to go through all selects on one form.

Hope to hear from you
Michal Cibor

mcibor

9:15 pm on Jun 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I found the problem now. Omitting the form was a right thing to do. Now the code looks like this:

var sel = document.getElementsByTagName("select");

function js_saveTxt()
{
var f = sel[0].value + "%2" +
sel[1].value + "%2";
}


And this works.

Thanks all!
Michal Cibor