views:

134

answers:

2

I have a select list:

<select id="sel">
     <option>text1</option>
     <option>text2</option>
     <option>text3</option>
     <option>text4</option>
</select>

I want to delete all items, without for loop. I tried:

document.getElementById('sel').length = 0;

But this doesn't work in some browsers.
Any ideas?

Thanks

+6  A: 
document.getElementById( 'sel' ).innerHTML = '';

Moved from comment:

You should understand such a length property more like a read-only property that gives you information about the state of the object. Like when you get the length of an array. Changing that value however doesn't affect the actual object, while removing an element correctly will cause the browser to re-render the DOM for that element (and as such update the value). I guess some browsers might interpret setting a length to zero will clear that object but in general that shouldn't be the expected behaviour. Also I think Element.length actually isn't part of the DOM specification.

To add some references to that, the core DOM Element doesn't have any length parameter. Both the HTMLSelectElement and the HTMLOptionsCollection (which can be accessed via HTMLSelectElement.options) have a length attribute but setting it should raise a DOMException.

So in both ways setting the length is illegal by the standard and as such should not be used if you want to support most browsers.

poke
@poke thanks man, it works:)could you explain why some browsers dont "understand" length method?
Syom
@Syom: You were trying to set the length of the `select` element; you needed to set the length of its `options` array (see my answer for an example, but the `innerHTML` version works just fine as well).
T.J. Crowder
@poke: assigning `0` to `length` will indeed mutate arrays. `length` is also part of the DOM 1 spec for `HTMLCollection`: http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-75708506 You are right that it is marked as read-only.
Crescent Fresh
I moved my comment to the answer and added some references.
poke
@poke: Setting the `length` property *on a DOM element* is indeed pointless, as it doesn't have one. Setting the `length` property on an *array* is absolutely a valid and correct way to change its length; see section 15.4 of the spec (http://www.ecma-international.org/publications/standards/Ecma-262.htm): *"...whenever the `length` property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted.."
T.J. Crowder
+4  A: 

Either of these work:

document.getElementById('sel').options.length = 0;

or

document.getElementById('sel').innerHTML = "";
T.J. Crowder
@T.J. Crowder document.getElementById('sel').options.length = 0; i tested, doesn't work
Syom
@Syom: has always worked for me, in any browser I've ever tried: http://jsbin.com/okaqe What are you using?
Crescent Fresh
@Crescent Fresh doesn't work in Google Chrome
Syom
@Syom: works for me, Chrome 2, 3 and 4 (don't have 1.0), all Windows Server 2003. What OS are you using?
Crescent Fresh
@Crescent Fresh Windows Seven. Google Chrome 4.1.249.1064 can the operation system have any influence?
Syom
Works fine for me in Chrome 4.1.249. `options.length= 0` is the standard canonical way to empty a select going back as far as ancient Netscape 2, so it would be a surprise if some browser broke it.
bobince
@Syom: I'm Chrome 4.1.249.1064 too. Anyone else got Win7 around to test? http://jsbin.com/okaqe
Crescent Fresh
@Crescent Fresh i've tested it just now! it deletes items part by part!only after four clicking it deletes all items.
Syom
@Syom: I can't imagine the OS makes a difference, no, but I have a Win7 machine I can install Chrome on later to see. It works on Chrome 4 (and IE, and FF) on WinXP -- I think perhaps your test case isn't quite right.
T.J. Crowder
@Syom: ok, are you serious? With that exact test link or in your own test sample?
Crescent Fresh
Works for me in all the browsers on my machine: FF36, IE8, Opera 9, Opera 10, Safari 4, Chrome (latest). I agree that this is the way to trim the options list since forever.
Mr. Shiny and New
@Crescent Fresh of course i'm serios!!!
Syom
@Syom: Put Chrome 4 on my Win7 machine, and it works there too; this is very old stuff, I'd've been shocked if it were broken. Live version: http://jsbin.com/ivule3 Pastie: http://pastie.org/967162 In that, I avoided using any libraries and kept it simple.
T.J. Crowder