Remove multiple items from listbox in Javascript [Chatakondu Gallery]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Suresh

    #1

    Remove multiple items from listbox in Javascript [Chatakondu Gallery]

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

    function fnRemoveListIte m(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.eleme nts.length;i++)
    {
    var e = obj.elements[i];
    var elementName = e.name.toString ()
    if (elementName.in dexOf(ctrlListB ox) >= 0 )
    {
    var objctrlListBox = e;
    break;
    }
    }
    for (var i = 0; i < objctrlListBox. length; i++)
    {
    if (objctrlListBox .options[i].selected)
    {
    objctrlListBox. remove(objctrlL istBox.selected Index);
    i--;
    }
    }
    return;
    }
    }
  • Richard Cornford

    #2
    Re: Remove multiple items from listbox in Javascript [Chatakondu Gallery]

    Suresh wrote:
    <snip>[color=blue]
    > if (formName == '')
    > var formName = document.forms( 0).name[/color]
    ^ ^
    Very few browsers understand parenthesise used in a property accessor
    context. They all understand bracket notation when used for the task.
    [color=blue]
    > obj = eval("document. " + formName)[/color]
    <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.


    Comment

    Working...