Finding items by name...

J

jodleren

Hi

I want the code below to find checkboxes only with a name, say
starting with "blabla_" (I have a number after that). How do I do
that?

WBR
Sonnich

function SelectAll(value)
{
form1 = document.forms[0];
for(j = 0; j < form1.elements.length; j++)
{
if(form1.elements[j].name.toLowerCase() == 'checkbox')
form1.elements[j].checked=value;
}
}
 
J

jodleren

Hi

I want the code below to find checkboxes only with a name, say
starting with "blabla_" (I have a number after that). How do I do
that?

Found it

function SelectAll(value)
{
form1 = document.forms[0];
for(j = 0; j < form1.elements.length; j++)
{
if(form1.elements[j].name.search("message_")==0)
form1.elements[j].checked=value;
}
}
 
V

VK

Hi

I want the code below to find checkboxes only with a name, say
starting with "blabla_" (I have a number after that). How do I do
that?

WBR
Sonnich

function SelectAll(value)
{
form1 = document.forms[0];
for(j = 0; j < form1.elements.length; j++)
{
if(form1.elements[j].name.toLowerCase() == 'checkbox')
form1.elements[j].checked=value;
}
}

if(form1.elements[j].name.indexOf('blabla_')!=-1)
 
J

jodleren

I want the code below to find checkboxes only with a name, say
starting with "blabla_" (I have a number after that). How do I do
that?
      if(form1.elements[j].name.toLowerCase() == 'checkbox')
if(form1.elements[j].name.indexOf('blabla_')!=-1)

What is the difference?

Stupid question, but I am not that much into JS
 
T

Thomas 'PointedEars' Lahn

jodleren said:
I want the code below to find checkboxes only with a name, say
starting with "blabla_" (I have a number after that). How do I do
that?

if(form1.elements[j].name.toLowerCase() == 'checkbox')

if(form1.elements[j].name.indexOf('blabla_')!=-1)

What is the difference?

The second approach is a) more straightforward than your solution, but
b) implemented wrong.

(a) You are not actually searching for a substring, so
String.prototype.search() seems inefficient.

(b) However, the implementation above is wrong because it is
only testing whether or not the name contains the string;
it does not make sure that the name begins with that string.
For that, it would have to be

if (form1.elements[j].name.indexOf('blabla_') == 0)

Another solution would make use of XPath:

var myCheckboxes = document.evaluate(
'//input[@type="checkbox" and starts-with(@name, "blabla_")]',
form1,
null,
7,
null
);

for (var j = myCheckboxes.snapshotLength; j--;)
{
myCheckboxes.snapshotItem(j).checked = value;
}
Stupid question, but I am not that much into JS

It is rather a matter of program logic.


PointedEars
 
V

VK

(b) However, the implementation above is wrong because it is
only testing whether or not the name contains the string;
it does not make sure that the name begins with that string.
For that, it would have to be

if (form1.elements[j].name.indexOf('blabla_') == 0)

Very right correction.
 
T

Thomas 'PointedEars' Lahn

Michael said:
jodleren said:
if(form1.elements[j].name.search("message_")==0)

if(form1[j].name.match(/^messsage_/))

if (/^message_/.test(form1.elements[j].name))

would be more efficient and less error-prone.


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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top