0

This is my function to check which checkboxs are checked.

function updateTagChecked() {
         var list_tag_selected = [];//to store the value of checkboxs selected.
     $('input[name=tag]:checked').each(function() {
         list_tag_selected.push($(this).val());
        });
     $('#tag_checked').val(list_tag_selected.join("&"))//Append this list to hidden field

    }
    //When are checkbox are click?
    $(function() {
         $('#collapse_option input').click(updateTagChecked);
         updateTagChecked();
     });

I want to do the same thing for multi select

<div id collapse_option>
    <li><label for="id_city">City:</label> 
   <select multiple="multiple" name="city" id="id_city">
    <option value="1">Phnom Penh</option>
    <option value="2">Takeo</option>
    <option value="3">Kampot</option>
    <option value="4">Kampongthom</option>
    <option value="5">Siemreip</option>
    <option value="6">pursat</option>
    <option value="7">preyveng</option>
    </select></li>
  </div>
<input type="hidden" id="store_multiselected" name="store_multiselected" value=""/>

In multi select Can we do this.?I want to store value for every selected in multi select in hidden field as I mention above.

2 Answers 2

1

When its a multiselect, the val() is already a list:

function updateTagChecked() {
    var list_tag_selected = $('#id_city').val();     // first pull values from multiselect
    $('input[name=tag]:checked').each(function() {
        list_tag_selected.push($(this).val());       // then from other checkboxes
    });

    //Append this list to hidden field
    $('#tag_checked').val(list_tag_selected.join("&"));
}

//When are checkbox are click?
$(function() {
     $('#collapse_option input').click(updateTagChecked);
     updateTagChecked();
});
Sign up to request clarification or add additional context in comments.

1 Comment

var list_tag_selected = $('#id_city').val(); // first pull values from multiselect Thanks for this point.:)
1

Yes, just use the selector select[name=city] option[selected] instead of input[name=tag]:checked.

1 Comment

you mean Should I change to or some thing else? $('input[name=tag]:checked') =$('select[name=city] option[selected]'

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.