If you think a bug might affect users in the 57 release, please set the correct tracking and status flags for Release Management.

Error when calling listbox.getIndexOfItem

VERIFIED INVALID

Status

()

Core
XUL
VERIFIED INVALID
7 years ago
7 years ago

People

(Reporter: Alexey Romanov, Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

7 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11 ( .NET CLR 3.5.30729; .NET4.0E) XPCOMViewer/0.9.2
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11 ( .NET CLR 3.5.30729; .NET4.0E) XPCOMViewer/0.9.2

Calling listbox.getIndexOfItem on any XUL listbox gives an error.

Reproducible: Always

Steps to Reproduce:
call [code]getIndexOfItem[/code] on any XUL listbox. Here is an example which can be run in XUL Editor:
[code]<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="yourwindow" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:h="http://www.w3.org/1999/xhtml">
 <listbox id="theList">
   <listitem label="Ruby"/>
 </listbox>

<script type="text/javascript">
<![CDATA[
var listbox = document.getElementById("theList");
listbox.appendItem(listbox.getIndexOfItem("Ruby"));
]]>
</script>
</window>[/code]
Actual Results:  
Received this error message in Error Console

Error: Could not convert JavaScript argument arg 0 [nsIListBoxObject.getIndexOfItem] = NS_ERROR_XPC_BAD_CONVERT_JS
Source file: chrome://global/content/bindings/listbox.xml
Line: 769

The listbox in preview window has one element:
Ruby

Expected Results:  
No error message, two elements in listbox:
Ruby
0
(Reporter)

Comment 1

7 years ago
Ok, sorry for stupidity. Apparently listbox.getIndexOfItem("Ruby") isn't looking for the element with the _label_ "Ruby", but the actual XUL list item object "Ruby", which obviously doesn't exist.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → INVALID

Comment 2

7 years ago
Verified invalid.

https://developer.mozilla.org/en/XUL/listbox#m-getIndexOfItem

Yep. The getIndexOfItem method takes a reference to a listitem as a parameter and returns its index. The getItemAtIndex method does the opposite and takes an index and returns a reference to the listitem at that index.

You need to pass getIndexOfItem a reference to the element itself, which you could get from document.getElementById if you give it an ID or by using document.querySelector or some other method.

By the way, this is not a forum so BBcode such as "[code]" will not work. For posting code testcases here, we generally recommend you attach the actual file(s) so they can be downloaded and run easily.
Status: RESOLVED → VERIFIED
OS: Windows 7 → All
Hardware: x86 → All
You need to log in before you can comment on or make changes to this bug.