Forum Moderators: open

Message Too Old, No Replies

getting an element by part of its name

         

sliad

11:15 am on May 26, 2004 (gmt 0)



I have some input elements. I want to get an element by using a method like "document.getElementsByName" but I want to give this function a part of the name string of the element.
for example: I have the element by the name: "N43:flag" ,
and I want to find it using the string: "flag" .

BlobFisk

11:33 am on May 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, sliad!

You could create an array of all elements with names and use this array to find the string you feed into the function.

You'd start of with something like this:


var allNames = new Array();
var allNames = document.getElementsByName("*");

So now you have an array (allNames) which contains all the named objects in your document.

You can then loop through all the array items:


for(1=0;a<allNames.lenght;i++)

And search for your string.

HTH

BlobFisk

2:34 pm on May 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Apologies, I've just noticed that the for loop should read:

for(i=0;a<allNames.lenght;i++)

Bernard Marx

7:27 am on May 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi BlobFisk,

Actually there's a couple more typos there:

for(i=0;[blue]i[/blue]<allNames.[blue]length[/blue];i++)

This line is excess to requirements:

var allNames = new Array(); 

Firstly,

allNames
would be assigned a new value, more importantly,
getElementsByName
doesn't return an array, but a collection.