2

I have a database table that I'm outputting onto a internal webpage. I need to find the value of a selected row so I can then merge the rows and send the value back to the db so it knows which rows to merge.

When clicking on a button to merge rows this is my function I have created:

    //merge the rows now
    function mergeTheRows() {
        //get value from each row
        $("#mergeRowsBody tr").each(function() {
            rowValue = document.getElementsByTagName("tr").value;
            mergeRowsArray.push(rowValue);
            console.log(mergeRowsArray);
        });
     }

Here is the body of the table (I'm only showing two rows for the example):

<tbody id="mergeRowsBody">
  <tr class="rowEditData odd mergeSelect" value="110461" role="row">
    <td class="mdl-data-table__cell--non-numeric sorting_1">ABBEVILLE</td>
    <td class="mdl-data-table__cell--non-numeric">South Carolina</td>
    <td class="mdl-data-table__cell--non-numeric">null</td>
    <td class="mdl-data-table__cell--non-numeric">null</td>
    <td class="mdl-data-table__cell--non-numeric">001</td>
    <td class="mdl-data-table__cell--non-numeric">39</td>
  </tr>
  <tr class="rowEditData even mergeSelect" value="107045" role="row">
    <td class="mdl-data-table__cell--non-numeric sorting_1">Abbeville County</td>
    <td class="mdl-data-table__cell--non-numeric">South Carolina</td>
    <td class="mdl-data-table__cell--non-numeric">001</td>
    <td class="mdl-data-table__cell--non-numeric">45</td>
    <td class="mdl-data-table__cell--non-numeric">null</td>
    <td class="mdl-data-table__cell--non-numeric">null</td>
  </tr>
 </tbody>

I keep getting undefined when it console logs to the browser. I've tried finding the value by ID and class as well, but it keeps saying undefined. What am I doing wrong?

All I need is to be able to save the ID of the row so I can then let the database know which rows will be merged.

1
  • Nenad and Vladu's answers both work great. This is the time I wish I could choose both answers. Commented Jan 17, 2017 at 17:04

3 Answers 3

2

You should use data- property instead of value, value is for inputs, selects... Also you need to add table in your html and then you can use map() and get() to return array of data-values.

var mergeRowsArray = $("#mergeRowsBody tr").map(function() {
  return $(this).data('value')
}).get();

console.log(mergeRowsArray)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody id="mergeRowsBody">
    <tr class="rowEditData odd mergeSelect" data-value="110461" role="row">
      <td class="mdl-data-table__cell--non-numeric sorting_1">ABBEVILLE</td>
      <td class="mdl-data-table__cell--non-numeric">South Carolina</td>
      <td class="mdl-data-table__cell--non-numeric">null</td>
      <td class="mdl-data-table__cell--non-numeric">null</td>
      <td class="mdl-data-table__cell--non-numeric">001</td>
      <td class="mdl-data-table__cell--non-numeric">39</td>
    </tr>
    <tr class="rowEditData even mergeSelect" data-value="107045" role="row">
      <td class="mdl-data-table__cell--non-numeric sorting_1">Abbeville County</td>
      <td class="mdl-data-table__cell--non-numeric">South Carolina</td>
      <td class="mdl-data-table__cell--non-numeric">001</td>
      <td class="mdl-data-table__cell--non-numeric">45</td>
      <td class="mdl-data-table__cell--non-numeric">null</td>
      <td class="mdl-data-table__cell--non-numeric">null</td>
    </tr>
  </tbody>
</table>

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

Comments

2

getElementsByTagName returns a live HTMLCollection of elements with the given tag name.

rowValue = document.getElementsByTagName("tr").value;

replace with

rowValue =$(this).attr("value");

1 Comment

I've tried using ById and ByClassName and those don't work either. I'm not sure why I can't pull out the value. I've done it before. Does it have something to do with it being inside an .each function?
0

Have you tried using just jQuery?

rowValue = $(".mergeSelect").val();

2 Comments

Yes I have and it still says undefined
@JBaldwin sorry my review was a mistake. :(

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.