0

I'm using the Jquery multiselect checkbox plugin and am getting an error when converting the returned (CSV) values into an array. While this is not, strictly, a jQuery question it seems to throw an error on a standard js method. Heres what I have:

$(function(){
    $("select").multiselect({
         click: function(e){
            if( $(this).multiselect("widget").find("input:checked").length > 4 ){
                return false;
            }
        },
        close:function(evt, ui) {                   // Get the selected values upon close
            var which = $(this).attr('id');         // Find out which selectbox was open
            var checkedVals = $('#'+which).val();   // Get CSV string of checked options
            var valArray = checkedVals.split(',');  // convert CSV string to array
                for(a in valArray) {
                    currentBox = ('#'+which+'Box');         // Find current selectbox wrapper
                    var eHeight;                            
                    eHeight = $('p'+currentBox).height();   // Current wrapper height
                    $('p'+currentBox).height(eHeight+18);   // Add 18px to current wrapper for each Value
                    $('p'+currentBox).append('<div style="line-height:18px; margin-left:90px;"><a href="#"><img class="deleteVal" src="images/closewin.png" align="texttop" border="0"></a> '+valArray[a]+'</div>');
                }
        }
    }).multiselectfilter();

I get back a comma separated string which I want to put into an array using js. The error - "checkedVals.split is not a function" - is thrown at this line: var valArray = checkedVals.split(',');

user1415445
  • 263
  • 7
  • 18
  • What is `checkedVal`? Does an element with ID represented by `which` exist? As IDs are unique, `$("#" + which)` should actually just be `$(this)`. Could you post some markup? – pimvdb Aug 23 '12 at 11:38
  • checkedVals is a mulitselctc mthod which returns the csv string... I get that back okay. Yes... each selectbox has a unique ID... Actually everything is working perfectly foa any number of select boxes. My ONLY problem is the split method. Maybe it has to be moved outside the multiselect "close" function?? – user1415445 Aug 23 '12 at 11:47

1 Answers1

1

You have to do 2 things

  1. Make sure you have set multiple property in html

    <select id="select" multiple="multiple">

  2. No need to convert to array. val will give the array ( NOT CSV ) of selected option values.

    var valArray = $('#' + which).val();

demo : http://jsfiddle.net/diode/32h2g/3/

Diode
  • 24,570
  • 8
  • 40
  • 51
  • Actually no... it does not return an array. Per the specs (and what I'm getting back) it returns a comma separated list: – user1415445 Aug 23 '12 at 12:26
  • did you check my js fiddle ? see the log in console – Diode Aug 23 '12 at 12:27
  • also see this http://stackoverflow.com/questions/2543322/get-value-of-multiselect-box-using-jquery-or-javascript – Diode Aug 23 '12 at 12:31
  • Yes.. did check your fiddle. What is displaying is a comma separated list. I'll check the other links you sent... thanks. – user1415445 Aug 23 '12 at 12:35
  • Oh.. now I understand.. What you see in the alert looks like CSV. But actually alert converts the array to string. So in your code `checkedVals` is actually array. You can directly use it. – Diode Aug 23 '12 at 12:46
  • Always use debugger tools like firebug and use `console.log` to see the value of variables. – Diode Aug 23 '12 at 12:47
  • Okay... thanks. I was missing the [a] in my appaned line. Works now... thanks! – user1415445 Aug 23 '12 at 12:50