Remove multiple items from listbox in Javascript [Chatakondu Gallery]

Discussion in 'Javascript' started by Suresh, Apr 16, 2004.

  1. Suresh

    Suresh Guest

    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;
    }
    }
    Suresh, Apr 16, 2004
    #1
    1. Advertising

  2. Suresh wrote:
    <snip>
    > 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.
    Richard Cornford, Apr 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. K B
    Replies:
    2
    Views:
    664
  2. Adrian Forbes

    Remove multiple selected items listbox

    Adrian Forbes, Aug 7, 2003, in forum: ASP General
    Replies:
    0
    Views:
    124
    Adrian Forbes
    Aug 7, 2003
  3. mc
    Replies:
    0
    Views:
    231
  4. Suresh
    Replies:
    0
    Views:
    168
    Suresh
    Apr 16, 2004
  5. Suresh
    Replies:
    2
    Views:
    396
    Suresh
    Apr 22, 2004
Loading...

Share This Page