Remove multiple items from listbox in Javascript [Chatakondu Gallery]

S

Suresh

formName: Name of the form
ctrlListBox: Name of the Listbox control

function fnRemoveListItem(formName, ctrlListBox)
{
if (formName == '')
var formName = document.forms(0).name
obj = eval("document." + formName)

if (confirm("Are you sure you want to remove this item?"))
{
for (var i=0;i<obj.elements.length;i++)
{
var e = obj.elements;
var elementName = e.name.toString()
if (elementName.indexOf(ctrlListBox) >= 0 )
{
var objctrlListBox = e;
break;
}
}
for (var i = 0; i < objctrlListBox.length; i++)
{
if (objctrlListBox.options.selected)
{
objctrlListBox.remove(objctrlListBox.selectedIndex);
i--;
}
}
return;
}
}
 
R

Richard Cornford

Suresh wrote:
if (formName == '')
var formName = document.forms(0).name
^ ^
Very few browsers understand parenthesise used in a property accessor
context. They all understand bracket notation when used for the task.
obj = eval("document." + formName)
<snip>

The use of - eval - is always unnecessary with property accessors. See:-

<URL: http://jibbering.com/faq/#FAQ4_39 >

The logic of the preceding 3 lines could be written as:-

obj = document.forms[(formName || 0)];

Incidentally, posting tab indented code on Usenet is counter productive
as some newsreaders collapse tabs completely (removing indentation),
while others will use default numbers of spaces per tab that are to
large for reasonable code presentation. Instead tabs should be converted
to a suitable number of spaces prior to positing. It is all covered in
the group's FAQ.

Richard.
 

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
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top