Focal Point
Problems with multiselect listbox in HTML

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/2701087602

January 12, 2007, 08:52 AM
Håkan
Problems with multiselect listbox in HTML
Has anybody found out how to handle the listbox with multiselect?
Assume the listbox1_onclick function is as follows:
alert(this.listbox1.options.selectedIndex);

If I click on eg. row no 2 in a listbox it says 0 (wrong) at first attempt, but on the second attempt it says 1 (correct).

Any help is appreciated.


WebFOCUS DS 8.0.06/08 DS/AS
WebFOCUS RS 8.0.08 (Linux/IBM i)
WebFOCUS Client 8.0.06 (Linux)
January 12, 2007, 03:03 PM
Fernando
Since you have a multi select box you have to loop thru the entire checking which is selected
This code will do it:

for (i=0; i if (this.listbox1.options[i].selected) {
alert(i);
}
}

Fernando


Prod WF 8.1.04, QA WF 8.2.03, Dev WF 8.2.03
January 12, 2007, 05:28 PM
Håkan
Fernando, a little bit closer. First I get an alert with the value 0 and when I close that one another alert pop up with the correct value. What causes the first alert to pop up?

I'm on WF 7.1.4 running on localhost on an XP box with Tomcat 5.0.


WebFOCUS DS 8.0.06/08 DS/AS
WebFOCUS RS 8.0.08 (Linux/IBM i)
WebFOCUS Client 8.0.06 (Linux)
January 12, 2007, 06:46 PM
Kerry
Hi H Lidholm,

One of our people reviewed the code and here is his suggestion.

This works as expected as the JavaScript event you want to use, is the onChange event and not the onClick event. The below code will demonstrate the differences.

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
  <form name=form1 id=form1>
  <select multiple name=listbox1 id=listbox1 onChange=javascript:onJSChange(); onClick=javascript:onJSClick();>
       <option value=a>A</option>
       <option value=b>B</option>
       <option value=c>C</option>
       <option value=d>D</option>
       <option value=e>E</option>
       <option value=f>F</option>
   </select>
  </form>
<SCRIPT LANGUAGE=javascript>
       var objThis = document.form1.listbox1;
       function onJSClick() { alert('onclick='+objThis.selectedIndex); }
       function onJSChange() { alert('onchange='+objThis.selectedIndex); }
</SCRIPT>
</BODY>
</HTML>


Hope this helps. Big Grin

Cheers,

Kerry


Kerry Zhan
Focal Point Moderator
Information Builders, Inc.
January 13, 2007, 10:35 AM
Håkan
Kerry, thanks, that works fine, but there seem to be a problem with a listbox in a chain. For some reason the chaining seem to disable the onchange event. If I remove the chaining it works fine. Any ideas?

Håkan


WebFOCUS DS 8.0.06/08 DS/AS
WebFOCUS RS 8.0.08 (Linux/IBM i)
WebFOCUS Client 8.0.06 (Linux)
January 15, 2007, 04:56 AM
Tony A
Håkan,

The onchange event removal has been noticed before (sorry, can't locate the posts at the moment) but the way around it it to reassign the onchange event as part of the load procedure.

The code that I use is -
<SCRIPT id=OnloadHandler>
function OnLoad() {
<!--startibilines-->
UpdateData();
<!--endibilines-->
// Overcome a bug in IB's script that clears an onchange event for a select
SelCtrl = document.getElementById("SELECT1");
SelCtrl.attachEvent("onchange", createSelects);
}
// This function adds an event handler to a control
    function createSelects() {
       this.displayElement = window.event.srcElement;
// This next line contains what would normally be in your onchange event action
       Show_Selects(this.displayElement.id);
    }
</SCRIPT>


lycka till!

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
January 16, 2007, 03:04 AM
Håkan
Tony,

thanks a lot. However, I decided to have a clickable text below the listbox instead, which will show all the selections made in the listbox using an alert message.

H


WebFOCUS DS 8.0.06/08 DS/AS
WebFOCUS RS 8.0.08 (Linux/IBM i)
WebFOCUS Client 8.0.06 (Linux)