1

I want to get the value of a table cell by click a button in the same row of this cell. this cell is in the previous column of the button's column, this is my trying code:

<table class="table table-bordered table-hover" >
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.CategoryId)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Name)
                        </th>
                        
                        <th>
                            @Html.DisplayNameFor(model => model.Quantity)
                        </th>
                        
                        
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Category.CategoryName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Name)
                            </td>
                            
                            <td>
                                @Html.DisplayFor(modelItem => item.Quantity)
                            </td>
                           
                            <td>
                                <div class="btn-group-sm">
                                    <button class="btn btn-primary" onclick="addArticle(this)">Add</button>
                                    
                                    <button class="btn btn-warning">Edit</button>
                                    
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

JS:

function addArticle(btn) {

    var RowNumber = btn.previousSibling.innerHTML;
    alert(RowNumber);
}

This code is not working,it displays undefined. Any help?

3
  • 1
    We need to see all of the relevant HTML in order to help you. I would guess that the previousSibling element to the clicked button will not be a td cell, though. If it is, then your HTML is invalid. Commented Mar 1, 2023 at 13:22
  • Could you share some more of your html code for context? Also, I see that you are referencing this which doesn't exist. You should use btn.previousSubling.... Commented Mar 1, 2023 at 13:23
  • you need to get your parent td of the button before doing previous: btn.parentElement.previousSibling Commented Mar 1, 2023 at 13:29

1 Answer 1

0

You need to walk the DOM tree:

  1. btn parent
  2. div parent
  3. table cell previous sibling
function addArticle(btn) {
   
    var RowNumber = btn.parentElement.parentElement.previousElementSibling.innerHTML;
    alert(RowNumber);
}
Sign up to request clarification or add additional context in comments.

Comments

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.