Jump to content

check a checkbox


Recommended Posts

Is there a way I can check a checkbox when I only know part of the name? For example I could have many checkboxes and they would all start with "email_" and then the username. I have a query to populate this checkbox. I can't have the form processing done after the query since when the page is reloaded it'll still show the records before I update. This is an approval all function which I send an email to all users approved. So what I want is when I press Approve All that all checkboxes are checked with javascript. This page is done in coldfusion if you know of another way of doing this let me know.EDIT: Please delete this post. I found the answer.

Link to post
Share on other sites

I had a similar situation where I needed to check the end of the control name, it was complicated by the fact that using Infragistics controls, they prepend a whole bunch of stuff to the control name as well. Here is one way:

function CheckCheckBoxes()	{		re = new RegExp("^email_")				e = new Enumerator(form1.elements)		for (;!e.atEnd();e.moveNext())		   		{			x = e.item();			if (x.name.match(re))				{				// form element found matching the name "email_" at the beginning			}		}	}

Link to post
Share on other sites

I could've used regexp but it was way simpler. The names were all the same, only the value was different. So I used a script to check them all before the form reloaded. Works great :)

Link to post
Share on other sites

After some searching around to see why I didn't know about the Enumerator object in javascript, I discovered that it isn't part of javascript. It's part of Microsoft's JScript so it'll only work in IE.Another way to do what aalbetski posted could be like this:

function CheckCheckBoxes(){	var re = new RegExp("^email_");	var inputs = document.getElementsByTagName("input");	var input;	for(var i = 0; i < inputs.length; i++)	{		input = inputs[i];		if(input.type == "checkbox")		{			if(input.id.match(re))			{				input.checked = true;				break;			}		}			}}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...