2

so this works fine

HTML

<p></p>
<select id="single">
    <option>Single</option>
    <option>Single2</option>
</select> 
<select id="multiple" multiple="multiple">
    <option selected="selected">Multiple</option>
    <option>Multiple2</option>
    <option selected="selected">Multiple3</option>
</select>

<div>
    <span class="label">Hobbies</span>
    <input type="checkbox" name="hobby" id="hel" value="hel">
    <label for="hel">hel</label>
    <input type="checkbox" name="hobby" id="pickle" value="pickle">
    <label for="pickle">Pickle eating</label>
    <input type="checkbox" name="hobby" id="walnut" value="walnut">
    <label for="walnut">Making walnut butter</label>
</div>

function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || [];
$( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + multipleValues.join( ", " ) );}

$( "select" ).change( displayVals );
displayVals();

http://jsfiddle.net/bfha4881/

but when I change the JQuery Selector to input it returs multipleValues.join() is not a function!

function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "input[name='hobby']:checked" ).val() || [];
$( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + multipleValues.join( ", " ) );
}

$( "input" ).change( displayVals );
displayVals();

so what am I doing wrong?

3 Answers 3

3

Try

var values = [];    
$( "input[name='hobby']:checked" ).each(function(){
    values.push($(this).val());
});
var str = values.join(", ");

Also, you can concatenate directly in the "each", but remember to remove the ", " from the last loop.

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

Comments

1

For input elements, the returned value will be string, so for array of checkboxes you can iterate over checked inputs and then use .map() to create an array as below

function displayVals() {
    var singleValues = $("#single").val();
    var multipleValues = $("#multiple").val() || [];
    var hobbies = $('input[name="hobby"]:checked').map(function () {
        return this.value;
    }).get();
    $("p").html("<b>Single:</b> " + singleValues +
        " <b>Multiple:</b> " + multipleValues.join(", ") +
        " <b>Hobies:</b> " + hobbies.join(", "));
}

$("select, input").change(displayVals);
displayVals();

Demo: Fiddle

2 Comments

can you please explain the last part of this: var multipleValues = $("#multiple").val() || []; does the: .val()|| [] indicates single or multiple value?
@Sam-Gh. if the select element is a multi select then .val() will return an array of selected values.. but if no values is selected then null is returned so || [] used to return an empty array if there are no elements selected
0

You need to get all checkboxes, you can do that with .each() function...

var checkBoxes = "";
function displayVals() {
  var singleValues = $( "#single" ).val();
  $( "input[name='hobby']:checked" ).each(function() {
        checkBoxes += $(this).val() + " ";
    });

  $( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + checkBoxes );
    checkBoxes = "";
}

Here is working example: jsfiddle

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.