conan conan - 5 months ago 16
Javascript Question

Trying to get all input value into string, jquery or javascript

I am trying to use jquery or javascript get all input value into a string (1,2,3,4) and ignore empty value. Thank you.

<form id="sym_form">
<input type="text" class="as_sym" id="sym_1" value="1">
<input type="text" class="as_sym" id="sym_2" value="2">
<input type="text" class="as_sym" id="sym_3" value="3">
<input type="text" class="as_sym" id="sym_4" value="4">
<input type="text" class="as_sym" id="sym_5" value="">


  // string to keep the result
  var output = "";
  // iterate over all inputs with class 'as_sym' inside the '#sym_form'
  $('#sym_form > input[class=as_sym]').each(function(){
    // only inputs with non-empty values                                                                                                        
    if ($(this).val() != "") {
      // the first value doesn't have a comma in front of it
      // subsequent values do have a comma in front of them
      if (output == "") {
        output += $(this).val();
      } else {
        output += "," + $(this).val();
  // here do whatever you want with the 'output' variable