Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is there a way to get the value from the first cell in html table row using javascript only?

I’m trying to figure out how to get the value from the first column of selected row inside the HTML table
i’m selecting the row using button created using this js code :

let tr = document.querySelectorAll("table tbody tr");

Array.from(tr).forEach(function (trArray) {
    let button = document.createElement("i");
    let td = document.createElement("td");
    button.innerText = "";
    button.className = "fa fa-eye";
    button.style.color = "black";
    button.hidden = "hidden";
    button.onmouseover = "this.style.cursor='pointer'";
    button.onclick = "viewrow(this)";
    button.setAttribute("onclick", "viewrow(this)");
    td.append(button);
    trArray.append(td);
});

the table is created from code behind using stringbuilder
what I tried is this:

StringBuilder sb = new StringBuilder();
                    sb.Append("<table id=" + "contractstable" + " runat=" + "server" + " " + "class=" + "table" + " >");
                    sb.Append("<caption>البنود</caption>");
                    sb.Append("<thead>");
                    sb.Append("<tr>");
                    sb.Append("<th>رقم البند</th>");
                    sb.Append("<th>رقم الفاتورة</th>");
                    sb.Append("<th>صنف البند</th>");
                    sb.Append("<th>سعر القطعة</th>");
                    sb.Append("<th>القيمة</th>");
                    sb.Append("<th>الخصم</th>");
                    sb.Append("<th>المجموع</th>");
                    sb.Append("<th>حذف</th>");
                    sb.Append("</tr>");
                    sb.Append("</thead>");
                    sb.Append("<tbody id=" + "mytbody" + ">");
                    foreach (DataRow dr in dt.Rows)
                    {
                        sb.Append("<tr><td data-label=" + "رقم-البند" + ">" + dr["contract_id"]
                    + "</td><td data-label= " + "رقم-الفاتورة" + " > " + dr["bill_id"]
                    + "</td><td data-label=" + "صنف-البند" + ">" + dr["contract_category"]
                    + "</td><td data-label=" + "سعر-القطعة" + ">" + dr["item_price"]
                    + "</td><td data-label=" + "القيمة" + ">" + dr["amount"]
                    + "</td><td data-label=" + "الخصم" + ">" + dr["discount"]
                    + "</td><td data-label=" + "المجموع" + ">" + dr["total"] + "</td></tr>");
                    }
                    sb.Append("</tbody>");
                    sb.Append("</table>");
                    ltrTable.Text = sb.ToString();

function getrowid(element) {
    var mytbody = document.getElementById("mytbody");
    var firstchild = mytbody.querySelectorAll("td:first");
    var allName = firstchild.value;
}

then i want to get the selected row id , what i have tried :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function deleterow(element) {
    var mytbody = document.getElementById("mytbody");
    var firstchild = mytbody.querySelector("td");
    var allName = firstchild.innerText;
    var hiddentext2 = document.getElementById("myrow_id");
    hiddentext2.focus();
    hiddentext2.value = allName;
    hiddentext2.blur();
}

>Solution :

You can use querySelector to get the first element. If it has more than one of similar elements, it always gets the first one.

Your cell does not have value either. You should use innerText instead.

function getrowid(element) {
  var mytbody = document.getElementById("mytbody");
  var firstchild = mytbody.querySelector("td");
  var allName = firstchild.innerText;
  console.log(allName)
}

getrowid()
<table>
  <tbody id="mytbody">
    <tr>
      <td>First</td>
      <td>Second</td>
    </tr>
    <tr>
      <td>Third</td>
      <td>Fourth</td>
    </tr>
  </tbody>
</table>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading