Collection of layers

G

Guest

Is there a Layers collection?

in the same way that i can access all the radio buttons

for(var i=0; i<form.radio.length; i++){
if(form.radio.checked){
var wch = form.radio.value
}
}

can I access all the layers on a page, with something like

for(var i=0; i<layers.length; i++){
if(layers.name.substr(0,2)=='c1'){
do something
}
}

obviously I know the above block of code will not work, but is their a
way of achieving this in javascript?????
 
G

Gregor Kofler

(e-mail address removed) meinte:
Is there a Layers collection?

in the same way that i can access all the radio buttons

for(var i=0; i<form.radio.length; i++){
if(form.radio.checked){
var wch = form.radio.value
}
}

can I access all the layers on a page, with something like

for(var i=0; i<layers.length; i++){
if(layers.name.substr(0,2)=='c1'){
do something
}
}

obviously I know the above block of code will not work, but is their a
way of achieving this in javascript?????


Maybe document.getElementsByTagName("div") is what are you looking for.

Gregor
 
G

Guest

Maybe document.getElementsByTagName("div") is what are you looking for.

Many many thanks, the following works just perfectly, had to change
"name" to "id"!

var myLayers = document.getElementsByTagName("div");
for(var i=0; i< myLayers.length; i++){
if(myLayers.id.substr(0,2)=='m1'){
document.getElementById(myLayers.id).style.backgroundColor =
nCol;
}
}
 
T

Thomas 'PointedEars' Lahn

Many many thanks, the following works just perfectly, had to change
"name" to "id"!

var myLayers = document.getElementsByTagName("div");
for(var i=0; i< myLayers.length; i++){

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

is more efficient than this if the value of myLayers.length does not change.
If iteration order also does not matter,

for (var i = myLayers.length; i--;)
{

is even more efficient than that.
if(myLayers.id.substr(0,2)=='m1'){


You might want to consider case-insensitive and/or more efficient matching
instead; either

if (myLayers.id.substr(0, 2).toLowerCase() == 'm1')
{

or

if (/^m1/i.test(myLayers.id))
{

Another more efficient possibility is using an XPath expression to retrieve
the list of nodes including and below the context node (`//'), e.g.

//div[@id^="m1"]

This can be used with document.evaluate(), e.g. in the Gecko HTML and XHTML
DOM, or, in the MSXML DOM, with XMLNode::selectNodes(). Google is your
friend. [psf 6.1]
document.getElementById(myLayers.id).style.backgroundColor = ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
nCol;


That approach is always unnecessarily inefficient. You have the reference
to the element object already, there is no need to retrieve it again:

myLayers.style.backgroundColor = nCol;


PointedEars
 
P

PointedEars

Thomas said:
Another more efficient possibility is using an XPath expression to
retrieve the list of nodes including and below the context node (`//'),
e.g.

//div[@id^="m1"]

This can be used with document.evaluate(), e.g. in the Gecko HTML and
XHTML DOM, or, in the MSXML DOM, with XMLNode::selectNodes(). Google
is your friend. [psf 6.1]

I have confused CSS 3 Selectors and XPath. In XPath 1.0, it must be

//div[starts-with(@id, "m1")]

As for case-insensitive matching:

//div[starts-with(@id, "m1") or starts-with(@id, "M1")]

XPath 2.0 provides a better way:

//div[fn:starts-with(fn:lower-case(@id)), "m1")]

But ISTM that is not implemented in Web browsers yet.


PointedEars
 
T

The Natural Philosopher

Maybe document.getElementsByTagName("div") is what are you looking for.

Many many thanks, the following works just perfectly, had to change
"name" to "id"!

var myLayers = document.getElementsByTagName("div");
for(var i=0; i< myLayers.length; i++){
if(myLayers.id.substr(0,2)=='m1'){
document.getElementById(myLayers.id).style.backgroundColor =
nCol;
}
}

Be aware that if you have a lot of divs this can get pretty slow. If you
are manipulating divs regularly.

I solved that by storing pointers to all the divs I wanted to use
regularly in a separate array. Ok each time you don't find one you have
to do the whole search, but its still quicker in the long run.
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
if(myLayers.id.substr(0,2)=='m1'){


You might want to consider case-insensitive and/or more efficient matching
instead; either

if (myLayers.id.substr(0, 2).toLowerCase() == 'm1')
{

or

if (/^m1/i.test(myLayers.id))
{


Of you are doing this for efficiency, regular expressions aren't really
that efficient. In the current crop of javascript engines, regexps
appear fast because they are implemented in tight native code, and the
javascript implementations aren't very fast themselves.

With more efficient javascript implementations like TraceMonkey,
SquirrelFish Extreme and V8, the overhead of regular expressions
begin to become visible (even if both SFX and V8 are now implementing
regexp compilers to native code).
RegExps are still the most powerfull way to process strings, but
they no longer beat simple javascript code.

/L
 
G

Guest

  for (var i = myLayers.length; i--;)
  {

never considered that way before, a little neater


   if(myLayers.id.substr(0,2)=='m1'){

  if (myLayers.id.substr(0, 2).toLowerCase() == 'm1')
  {


as I create the layers i see no need for the toLowerCase()

  if (/^m1/i.test(myLayers.id))
  {


test() is a new function for me, thanks!

as 'm1' will not be a string, it will be a variable that will hold
'm1', 'm2','c1' or 'c2'
how would i use a variable in the test function ?

document.getElementById(myLayers.id).style.backgroundColor = nCol;


That approach is always unnecessarily inefficient.  You have the reference
to the element object already, there is no need to retrieve it again:

    myLayers.style.backgroundColor = nCol;


Obviously better, I seem to struggle to get my head round objects and
variables, presumably
myLayers is an object ???

Many thanks for all that PointedEars, I am certainly a little wiser

stuart
 
G

Gregor Kofler

(e-mail address removed) meinte:
Obviously better, I seem to struggle to get my head round objects and
variables, presumably
myLayers is an object ???


A reference to a DOM element, which in turn is an object.

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
(e-mail address removed) meinte:
Obviously better, I seem to struggle to get my head round objects and
variables, presumably
myLayers is an object ???


A reference to a DOM element, which in turn is an object.


A reference to a DOM element *object* that represents an element in the
markup :)


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
if(myLayers.id.substr(0,2)=='m1'){

if (myLayers.id.substr(0, 2).toLowerCase() == 'm1')
{


as I create the layers i see no need for the toLowerCase()


The implementation of case-sensitiveness in HTML UAs as specified in
HTML 4.01 is heavily overrated.

if (/^m1/i.test(myLayers.id))
{


test() is a new function for me, thanks!

as 'm1' will not be a string, it will be a variable that will hold
'm1', 'm2','c1' or 'c2'
how would i use a variable in the test function ?


Let the global `s' be the prefix to be searched for. Quick hack:

function regexp_escape(s)
{
return s.replace(/[]\\{}?+*[]/g, "\\$&");
}

if (new RegExp("^" + regexp_escape(s), "i")
.test(myLayers.id))

(RTFM, STFW.) Of course, the more general the RegExp matching needs to be,
the less efficient it becomes as compared to simple string comparison.
Many thanks for all that PointedEars, I am certainly a little wiser

You are welcome.

What problems do you have to put at least this in your From header, so that
one doesn't have to reply to an e-mail address instead of a real person?


PointedEars
 
G

Guest

What problems do you have to put at least this in your From header, so that
one doesn't have to reply to an e-mail address instead of a real person?

PointedEars

I didn't know we were real people :)

Seriously though, I have tried, I can't get my name to appear. My name
is in the "My Profile" bit see this screen shot, but it won't appear
on my posts.
http://www.corbetteer.co.uk/public/images/temp.jpg
i've given up
 
T

Thomas 'PointedEars' Lahn

I didn't know we were real people :)
*g*

Seriously though, I have tried, I can't get my name to appear. My name
is in the "My Profile" bit see this screen shot, but it won't appear
on my posts.

As I have said before, it won't appear in your postings until you have
*subscribed* to the newsgroup
(<http://groups.google.com/group/comp.lang.javascript/subscribe>, in case
you can't find the link). BTDT, so I know for sure that it works.


F'up2 poster

PointedEars
 

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
474,116
Messages
2,570,699
Members
47,274
Latest member
SeleneHgw8

Latest Threads

Top