Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:54:41+00:00 2026-05-26T08:54:41+00:00

I need the text value of selected options in a multiple select returned as

  • 0

I need the text value of selected options in a multiple select returned as a “sentence” – just to join the strings, separated by a comma and a space.

The HTML generated for the select looks like this:

<select id="foo_bazs_attributes_0_bar_ids" name="foo[bazs_attributes][0][bar_ids][]" multiple="multiple" data-validate="true">
  <option value="1">I</option>
  <option value="2">Want</option>
  <option value="3">a</option>
  <option value="4">Sentence</option>
</select>

So I need my function to return “I, Want, a, Sentence”.

To do this with jQuery, I found the following (at http://calisza.wordpress.com/2009/03/29/6-jquery-snippets-you-can-use-to-manipulate-select-inputs/):

var foo = [];
$('#multiple :selected').each(function(i, selected){
 foo[i] = $(selected).text();
});

so I wrote the following in my CoffeeScript file:

$.fn.selectedToSentence = () ->
  arr = []
  $('this :selected').each (i, sel) ->
    arr[i] = $(sel).html()
  arr.join(", ")

but it never returns the string.

— edit —

I am calling it like:
$(‘select[id$=”_bar_ids”]’).selectedToSentence()

— /edit —

— edit 2 —

at the CoffeeScript site, it shows the above script as compiling to:

var $;
$ = jQuery;
$.fn.selectedToSentence = function() {
  var arr;
  arr = [];
  $('this :selected').each(function(i, selected) {
    return arr[i] = $(selected).text();
  });
  return arr.join(", ");
};

— /edit 2 —

I have also tried the following syntaxes:

$(@ :selected).each ...
$('@ :selected').each ...
$("#{@} :selected").each ...

all without success.

I’ve Googled, and looked around SO, perhaps with the wrong search terms, because I’m coming up empty.
I feel like it’s just a syntactical thing. What am I doing incorrectly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:54:42+00:00Added an answer on May 26, 2026 at 8:54 am

    This isn’t a CoffeeScript issue, it’s just a plain old jQuery issue. In particular “'this :selected'” isn’t a valid selector in CSS or jQuery (this is a variable in JavaScript but it’s meaningless in a selector string). If you want to use a CSS selector only on children of a certain element (like this, in your scenario) you can pass that element as the second parameter to $, e.g.:

    $(':selected', this).each ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a <select multiple='multiple' and I need to show the selected value in
I have two tables with a weak relation. I need get a text value
An XSLT-newbie problem: I need to substitute a text value in XML-file. All other
I have a multiple select, and I need to force the user to choose
I need to get the value of the selected option (when changed) in a
To get the selected value from a dropdown with ID is easy: $(#ComboBox option:selected).text()
How can I get the selected item's value and text in JavaScript? This is
How do I get the actual value (or text) of the item selected in
easy one guys: i have a select , type multiple , so i need
I need to change some text values inside an after effect project file that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.