How to remove empty spaces in JS var?

How can I remove empty space in the var con_str after I join the push value?


<input type="checkbox" id="chckBox1" onchange="fnTest(this);" /> <a class="ref"> Banana </a>
<input type="checkbox" id="chckBox2" onchange="fnTest(this);" /> <a class="ref"> Apple </a>
<input type="checkbox" id="chckBox3" onchange="fnTest(this);" /> <a class="ref"> Orange </a>

<input type="button" id="testABC" onclick="submit()" value="OK" />


function fnTest(check) 

function submit() 
    var values = [];

    var con_str = ""+values.join();;

This is the output that I’ve right now if I select all three checkboxes.,%20Apple%20,%20Orange

The output that I wanted,Apple,Orange

I tried added con_str.trim(). But it didn’t work.

Here’s the fiddle,


Your <a>s’ contents have spaces on the ends. Trim them.


A more elegant method, using .map instead of .push:

const values = [...$("input[type=checkbox]:checked")]
  .map(input => input.nextElementSibling.textContent.trim());