darwinist wrote:
RobG wrote:
darwinist wrote:
RobG wrote:
>darwinist wrote:
>>
>>Brian D wrote:
>>>I have a multiple select list that is created dynamically based on a
>>>previous selection on an asp page. The first thing I do is to clear
>>>the curent option list by
>>>>
>>>document.for m1.itemcross.le ngth = 0;
>>>>
>>>The only problem is that it leaves the optgroups. How do I also get
>>>rid of the optgroups?
[...]
>>You need an id for the object or a reference to it:
>The OP has already indicated how he's doing that, and may be using
>either an ID or a NAME attribute.
[...]
>> // delete an object by reference
>> function del(element){el ement.parentNod e.removeChild(e lement);}
>The OP is attempting to remove the child nodes, not the element itself.
>
Isn't an optgroup an element that you can remove like any other?
Yes. Your response was essentially to give every option an ID, then
remove them one by one using getElementById. That is not a reasonable
method given the question.
I said "or a reference to it", and gave commented, working examples of
how to deal with both. What's your problem?
The OP already had a reference to the select element and just wanted to
remove all the child nodes. To use your proposed solution, the OP
would have looped through all the child nodes, then called the 'del'
function which used the child node to reference back to the parent node
to delete itself.
That may have lead to a few characters less in the for loop, but also
an extra unnecessary function object plus an extra couple of loop-ups
for parent and child nodes. So appart from obfuscation, you also make
the whole process less efficient.
The function I posted was perhaps 3 lines of code and could (had the OP
wanted) be wrapped in a separate 'deleteAllChild Nodes' function. I
think it actually required fewer keystrokes, not that it matters.
Incidentally, the fastest way I've seen to delete all the child nodes
of an element is to replace it with a shallow clone of itself.
Unfortunately, a few scarce browsers don't like doing that with all
elements so it's not useful on the web. But for an intranet... ;-)
Javascript's native methods use a lot of codespace for common things
that don't take much time. This rigid structure is important to the
integrity of the platform but when you are putting it to any actual
purpose you need short, clear, purpose-specific functions that reflect
what your application, not the language, is doing.
I don't see how adding an ID to every element you want to delete makes
life easier. It also suggests managing all those IDs and some
algorithm to work out which ones are of interest.
Creating single-line functions purely for the sake of reducing the
number of keystrokes for a programmer to type a method will not lead to
any great advantage in reducing software development times (that
discussion is being hosted in another thread I think). If it did, such
widely used environments as VB wouldn't have names that approach the
length of short sentences (please don't assume I think VB is some
paragon of programming excellence - it's just an example) and brevity
would be a fundamental principle of coding standards everywhere. It
isn't - clarity is.
If you've ever tried to maintain some one else's code (say C or C++)
you'd know why.
--
Rob