1

Hello I am new in PHP and JavaScript. I have a code of Dropdown Checkbox. I want to try get out values of checked options with comma separate like 1,2,3

My problem is that when i run my code my output have one extra comma at the end like 1,2,3, and my desired output is 1,2,3

Here is my code

HTML Part

<select id="agency" multiple="multiple">
<?php
if (is_array($rating_agencies) && !empty($rating_agencies)) {
    foreach ($rating_agencies as $rating_agencie) {
        echo '<option value="'.$rating_agencie->ID.'"';
        echo '>';
        echo $rating_agencie->name;
        echo '</option>';
    } 
}           
?>
</select>
<input type="button" id="btnSelected" value="Get Selected" />

Java Script

<script type="text/javascript">
    $(function () {
        $('#agency').multiselect({
            includeSelectAllOption: true
        });
        $('#btnSelected').click(function () {
            var selected = $("#agency option:selected");
            var message = "";
            selected.each(function () {
                message += $(this).val() + ",";
            });
            alert(message);
        });
    });
</script>
7
  • In PHP you can use rtrim().. Commented Jun 14, 2016 at 7:08
  • 2
    You can use $.map() as var selected = $("#agency option:selected").map(function() { return $(this).val(); }).get(); Commented Jun 14, 2016 at 7:09
  • @FrayneKonok where i use this? Commented Jun 14, 2016 at 7:09
  • I would probably check if the message is empty and if it's false i wouldnt provide a comma, otherwise fist appenda comma,t hen the value. Commented Jun 14, 2016 at 7:09
  • 1
    stackoverflow.com/questions/2047491/how-to-remove-last-comma Commented Jun 14, 2016 at 7:10

6 Answers 6

2

Use jQuery.map with Array#join

.get() will return basic-array instead of array-of-objects

$(function() {
  $('#agency').multiselect({
    includeSelectAllOption: true
  });
  $('#btnSelected').click(function() {
    var message = $("#agency option:selected").map(function() {
      return this.value;
    }).get();
    alert(message.join(','));
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use slice to remove the last comma.

$('#btnSelected').click(function () {
    var selected = $("#agency option:selected");
    var message = "";
    selected.each(function () {
        message += $(this).val() + ",";
    });
    message = message.slice(0, -1);
    alert(message);
});

This is your question solution, OR you can go with @Rayon.

Comments

1

Use slice function :

<script type="text/javascript">
    $(function () {
        $('#agency').multiselect({
            includeSelectAllOption: true
        });
        $('#btnSelected').click(function () {
            var selected = $("#agency option:selected");
            var message = "";
            selected.each(function () {
                message += $(this).val() + ",";

            });
            message = message.slice(0, -1);
            alert(message);
        });
    });
</script>

Comments

0

Try to get the value instead of option:selected, It may work for you

var selected = $("#agency").val();

Comments

0

use rtrim method in php

// $commaString = "1,2,3,";
  $string = rtrim($commaString,",");
  // output 
  // 1,2,3

in Javascript

var comma_string = "1,2,3,";
string = comma_string.replace(/,+$/,'');

You can use in either side as your logic.

Comments

0

Just use $rating_agencies = array_filter($rating_agencies) before your "if" statement.

Comments

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.