Search code examples
javascriptjqueryhtmlthymeleaf

How to get current value of td usig javascript.?


I m iterating td values in html using thymeleaf. I create my webpage such that , there are view,approve.disapprove buttons for each iterated element of td. Now when a user presses VIEW button , i need to access the particular td value corresponding to it. Any solutions on how to access the td value in javascript.?

I currently usee this in js. but it returns the entire list value . not the particular value.

 var r = $("#testing #InputRuleName").text();
    alert(r);

Web Page Design:

 <ol>
            <table id="testing">
             <tr th:each="RuleNameList : ${model.RuleNameList}">                    
                    <br/>
                    <br/>
              <li>  <td th:id="InputRuleName" th:text="${RuleNameList}" />        
                    <button type="button" onclick="sendresponse()" id="view">VIEW</button>
                    <button type="button" onclick="sendresponse()" id="Approve">APPROVE</button>
                    <button type="button" onclick="sendresponse()" id="DisApprove">DISAPPROVE</button> </li> 
                    <br/>
                </tr>
                </table>
                </ol>

Solution

  • You can access it from the event object

    onclick="sendresponse(event)"
    
    function sendresponse(e) {
     console.log(e.target.parentElement)
    }