Hello,

As this forum has been so helpful before, this time it's a JS question:
I have a list of checkboxes from which the user can select 1 or more to check and perform an action upon.
Thus, all checkboxes have the same "name" (in ASP code, since it's DB-related):

response.write "<input type=checkbox name=""FieldName"" value="&obj("OrderID").value&">"

I call JS using a button:

<img src="images/TrashBin.png" border=0 onclick="Delete(document.FormName.FieldName)">

Which is supposed check which checkbox is checked using this JS code:

function DeleteEntry(field){
for (i = 0; i < field.length; i++) {
 if (field[i].checked) {
 action...
 }
}
}

Problem: if I have more than 1 checkbox, it works fine.
BUT if I have just 1 checkbox, both field.length and field.checked are "undefined" and so I can't check if the one checkbox is checked or not.

Any ideas?

Thanks in advance,

Asaf

Recommended Answers

All 3 Replies

var chkobj = document.fselall['FieldName'];
var i=0;
if(chkobj[i].checked == true)
alert(1);

Try to implement checkbox as an object, by using the above code.

Gdluck

It didn't work.
Though I couldn't seem to find document.fselall anywhere I tried that and the following as well:

var chkobj = document.all['fieldname'];
alert (chkobj.length);

and it didn't work - if there's only one checkbox, chkobj.length is undefined again.
Either I got it wrong or I need a different idea.

Thanks for the effort none-the-less!!

Asaf

A friend helped me out for a nice solution:

if (field.length === undefined) {
		if (document.List.CheckBox.checked){
		(action);
		}
	}
	else {
		for (i = 0; i < field.length; i++) {
			if (field[i].checked) {
			(action);
			}
		}
	}

Not beautiful, but effective.

Thanks for all your help,

Asaf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.