Jump to content
  • 0

Select multiple values from a listbox in a update DataPage


ppbb123

Question

3 answers to this question

Recommended Posts

  • 0

Try this:

<script>
function select_ (){

// get listbox element

var listbox = document.getElementById("EditRecordFIELD");
// get listbox length
var len = listbox.options.length;
// get selected element
var selected = listbox.options[len-1].value;
// verify if selected element contains comas, if yes, do the following
if (selected.indexOf(",") != -1 ){
// Split selected option into array
var m_array = new Array();m_array=selected.split (",");
// Delete last option from the listbox
listbox.options.remove(len-1);
// Go through listbox and select each option, if it is present in array
for( var i=0; i< m_array.length; i++ ){for( var j=0; j< len-1; j++ ){
// Trim spaces while comparing elements from array and listbox
if( m_array[i].replace(/^s+|s+$/g, ') == listbox.options[j].value ){
listbox.options[j].selected = "selected";
}}}}}
// Enable multiselect
for listboxdocument.getElementById("EditRecordFIELD").multiple="multiple";
// call select_ funtion when details page is loaded

window.onload = select_;
</script>

Replace FIELD in "EditRecordFIELD" with the actual field name.

Link to comment
Share on other sites

  • 0

If you have more than one listboxes on your details or update user this code

<script>
// Function for selecting corresponding elements from the list box.
function select_ (){
// get listbox element
var o_els = new Array("EditRecordFIELDNAME","EditRecordFIELDNAME2","EditRecordFIELDNAME3");
for (var k=0; k< o_els.length; k++ ) {
var o_el = document.getElementById(o_els[k]);
// Enable multiselect for listbox
o_el.multiple="multiple";

// get listbox length
var len = o_el.options.length;
// get selected element
var selected = o_el.options[len-1].value;
// verify if selected element contains comas, if yes, do the following
if (selected.indexOf(",") != -1 ){
// Split selected option into array
var m_array = new Array();m_array=selected.split (",");
// Delete last option from the listbox
o_el.options.remove(len-1);
// Go through listbox and select each option, if it is present in array
for( var i=0; i< m_array.length; i++ ){for( var j=0; j< len-1; j++ ){
// Trim spaces while comparing elements from array and listbox
if( m_array[i].replace(/^s+|s+$/g, '') == o_el.options[j].value ){
o_el.options[j].selected = "selected";
}
}
}
}
}
}
// call select_ funtion when details page is loaded
window.onload = select_;
</script>
Link to comment
Share on other sites

  • 0

Got it to work. Thanks to May for for this script.

There was a couple of small syntax error

Double quotes was missing in script

if( m_array.replace(/^s+|s+$/g, "") == listbox.options[j].value ){

 

Space after, comma was missing while splitting array.

 

Heres a working copy of the final script.

 

<script>
 var fieldName = "InsertRecordMultiListbox";
   var x=document.getElementsByName(fieldName);
   x[0].multiple=true;
function select_ (){


// get listbox element

var listbox = document.getElementById("InsertRecordMultiListbox");
// get listbox length
var len = listbox.options.length;
// get selected element
var selected = listbox.options[len-1].value;
// verify if selected element contains comas, if yes, do the following
if (selected.indexOf(",") != -1 ){
// Split selected option into array
var m_array = new Array();m_array=selected.split (", ");
// Delete last option from the listbox
listbox.options.remove(len-1);
// Go through listbox and select each option, if it is present in array
for( var i=0; i< m_array.length; i++ ){for( var j=0; j< len-1; j++ ){
// Trim spaces while comparing elements from array and listbox
if( m_array.replace(/^s+|s+$/g,"") == listbox.options[j].value ){
listbox.options[j].selected = "selected";
}}}}}


window.onload = select_;
</script>

 

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...