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(',');

2
  • 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? Commented Aug 23, 2012 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?? Commented Aug 23, 2012 at 11:47

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

7 Comments

Actually no... it does not return an array. Per the specs (and what I'm getting back) it returns a comma separated list:
did you check my js fiddle ? see the log in console
Yes.. did check your fiddle. What is displaying is a comma separated list. I'll check the other links you sent... thanks.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.