-1

I have a table and i am adding rows by cloning it. I want to know if there is any way by which i can get all the input and drop-down values from my table.(Using form.serialize() will give me unnecessary values which i don't need )

HTML

<table border="0" cellspacing="1" cellpadding="1" id="dataTable" name="dataTable" class="graphtable">
  <thead>

    <tr>
      <td class="headingalign" width="16%">Links</td>
      <td class="headingalign" width="32%">Desciption</td>
      <td class="headingalign" width="16%">Image</td>
      <td class="headingalign" width="16%">URL</td>
      <td class="headingalign" width="05%"></td>

    </tr>
  </thead>
  <tbody>

    <tr id="id0" class="vals" name="id0">

      <td>
      <div class="id_100">
    <select type="select-one" id='fldsearch' class="objselect" name="fldsearch" onChange="disableField(this)" >
        <option value="S">Select</option>
        <xsl:for-each select="faml/response/qlwidgetresponsedto/searchby/datamapdto">
            <xsl:sort order="ascending" select="description"/>
            <option value="#{description}">
                <xsl:value-of select="description"/>
            </option>
        </xsl:for-each>
        </select>
     </div> </td>
      <td>
        <input id="flddesc" name="flddesc" maxlength="500"  disabled="true" class="objinputtext1" size="85" value="{//RESPONSE}"  />

      </td>
      <td>
        <input  id="fldimg" name="fldimg" maxlength="50"  disabled="true" class="objinputtext2" size="45" value="{//RESPONSE}"  />

      </td>
      <td>
        <input id="fldurl" name="fldurl" maxlength="55" disabled="true" class="objinputtext3" size="40" value="{//RESPONSE}"  />

      </td>
      <td>
      <input tabindex="6" value="Delete Row" disabled="true" class="DeleteButton"  type="button" />
      </td>
    </tr>
  </tbody>
</table>
Saurav
  • 29
  • 8

1 Answers1

1

You can iterate all input and select elements from table and read values using .val() method.

See below code snippet

$(function(){
    var data = {};
    $('#dataTable input,select').each(function(){
       var id= $(this).attr('id');
       var val = $(this).val();
       data[id]=val;
    });
    console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<table border="0" cellspacing="1" cellpadding="1" id="dataTable" name="dataTable" class="graphtable">
  <thead>

    <tr>
      <td class="headingalign" width="16%">Links</td>
      <td class="headingalign" width="32%">Desciption</td>
      <td class="headingalign" width="16%">Image</td>
      <td class="headingalign" width="16%">URL</td>
      <td class="headingalign" width="05%"></td>

    </tr>
  </thead>
  <tbody>

    <tr id="id0" class="vals" name="id0">

      <td>
      <div class="id_100">
    <select type="select-one" id='fldsearch' class="objselect" name="fldsearch" onChange="disableField(this)" >
        <option value="S">Select</option>
        <xsl:for-each select="faml/response/qlwidgetresponsedto/searchby/datamapdto">
            <xsl:sort order="ascending" select="description"/>
            <option value="#{description}">
                <xsl:value-of select="description"/>
            </option>
        </xsl:for-each>
        </select>
     </div> </td>
      <td>
        <input id="flddesc" name="flddesc" maxlength="500"  disabled="true" class="objinputtext1" size="85" value="{//RESPONSE}"  />

      </td>
      <td>
        <input  id="fldimg" name="fldimg" maxlength="50"  disabled="true" class="objinputtext2" size="45" value="{//RESPONSE}"  />

      </td>
      <td>
        <input id="fldurl" name="fldurl" maxlength="55" disabled="true" class="objinputtext3" size="40" value="{//RESPONSE}"  />

      </td>
      <td>
      <input tabindex="6" value="Delete Row" disabled="true" class="DeleteButton"  type="button" />
      </td>
    </tr>
  </tbody>
</table>
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • Thanks for the answer but it seems like it always take the last values only. – Saurav Jan 20 '20 at 13:52
  • if name are same for multiple inputs then it will override in the `data` map. You can use unique ids instead of name or put values in array instead of map. Check updated answer using ids. – Bhushan Kawadkar Jan 20 '20 at 13:55