Cannot get value of a first TD in current row

tera_789

My requirement is to have 3 check-boxes in each table row, and once the checkbox is checked, it will create a hidden input with the value corresponding to the first <td> of THAT particular row.

Here is a sample row:

<tr>
    <td>user1</td>
    <td>768.51</td>
    <td>4,680</td>
    <td>0</td>
    <td>0%</td>
    <td>0</td>
    <td><input type="checkbox" name=">1 year and <=2 years[]" onclick="createUserNameInput()"></td>
    <td>0.00</td>
    <td>0.00%</td>
    <td>1</td>
    <td><input type="checkbox" name=">2 years and <=3 years[]" onclick="createUserNameInput()"></td>
    <td>768.51</td>
    <td>100.00%</td>
    <td>4,677</td>
    <td><input type="checkbox" name="> 3 years[]" onclick="createUserNameInput()"></td>
</tr>

Once any of the 3 check-boxes are checked, this hidden input should be created. If 3 are checked, 3 inputs should be created. <value> of these inputs should equal the value of first <td> in THIS row, which is user1. Here is how I am trying to implement it (code is inconsistent but I just need to get it working first):

<script>
    function createUserNameInput() {
        // var input = document.createElement("input");
        // input.setAttribute("type", "hidden");
        // input.setAttribute("name", "user");
        // input.setAttribute("value", $(this).parent().siblings(":first").text()); // VALUE IS EMPTY
        // document.getElementById("main-form").appendChild(input);

        alert($(this).parent().siblings(":first").text()); // VALUE IS EMPTY
    }
</script>

I have referenced these previous posts but none of those solutions work for me. When I run alert(), value is empty - nothing is displayed:

  1. Get first TD of THIS row
  2. Jquery- Get the value of first td in table

Who can help? If it can be done without jQuery, even more amazing!

Chaska
  1. You have to pass the object to the function you are calling:

    onclick="createUserNameInput(this)"

  2. Convert the DOM object to jQuery object you retrieve.

    createUserNameInput(e) and $(e).parent()...

function createUserNameInput(e) {
  // var input = document.createElement("input");
  // input.setAttribute("type", "hidden");
  // input.setAttribute("name", "user");
  // input.setAttribute("value", $(this).parent().siblings(":first").text()); // VALUE IS EMPTY
  // document.getElementById("main-form").appendChild(input);

  alert($(e).parent().siblings(":first").text()); // VALUE IS NOT EMPTY NOW!
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>user1</td>
    <td>768.51</td>
    <td>4,680</td>
    <td>0</td>
    <td>0%</td>
    <td>0</td>
    <td><input type="checkbox" name=">1 year and <=2 years[]" onclick="createUserNameInput(this)"></td>
    <td>0.00</td>
    <td>0.00%</td>
    <td>1</td>
    <td><input type="checkbox" name=">2 years and <=3 years[]" onclick="createUserNameInput(this)"></td>
    <td>768.51</td>
    <td>100.00%</td>
    <td>4,677</td>
    <td><input type="checkbox" name="> 3 years[]" onclick="createUserNameInput(this)"></td>
  </tr>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Not able to Get td value of Current Row on Button Click Inside The DIV

jquery get first td value

How to get TD from next row with same index as current TD

Cannot get value for first column on a selected row in listview

How to get first td of next row with JQuery?

Get the value of current table row

how to get selected td row value in javascript?

get all td value of same row by jquery

How to get value of a <td> onclick to an other <td> of a same row?

Get value of a <td> onclick to an other <td> of the same row with Angular

Get the first row value of textarea

Get a table row's first td ID and then map it

Get only the first child of every td in a row in jQuery

How to get the first two td in row using jquery

How to get the difference between first row and current row in pandas

Get previous value of current row in select

Get occurences of a value in column for current row and above

Get value next row based on value current row Selenium

Subtract current row value by previous row value and get balance amount to current row

Get the column value <td> from a selected row <tr> by the class Attribute

How to get Table td value on span click after that remove that row

Get row from a table by td class and specific value in the cell in jquery

How can I get td first child value?

How to get the first column/td value of a foreach loop

How to get table's first column td value in angular 6

Get first cell value of each row in table

Pandas - Get first row value of a given column

How to get the first row with value in range

Javascript & HTML : get the first value of an array row