...">

Jquery set value of all inputs inside a specific td

I have a table ...

        <table id="tblTransactions" class="table table-striped">
          <thead>
            <tr>
              <th class="col-md-1">Date</th>
              <th class="col-md-4">Description</th>
              <th class="col-md-2">Debit</th>
              <th class="col-md-2">Credit</th>
              <th class="col-md-3">Category</th>
            </tr>
          </thead>
          <tbody>
            <c:forEach items="${transactions}" var="element"> 
              <tr>
                <td><fmt:formatDate pattern="dd/MM/yyyy" value="${element.tranDate}"/></td>
                <td class="colTranDesc">${element.tranDescription}</td>
                <td>${element.debit}</td>
                <td>${element.credit}</td>
                <td class="colCategory"><input type="text" class="eetag" name="tag"></td>
              </tr>
            </c:forEach>              
          </tbody>
        </table>

After loading the page, I want to set a value for each entry for each td with the class "colCategory".

I would like the selector to look something like this, but I have problems with the syntax ...

$("#tblTransactions > tbody  > tr > td > input.colCategory").each(function(){
    var elt = $(this).val("Phone");
});

Can someone help me with this syntax please. Or, if there is an easier way to do this, I am open to suggestions.

THX

+4
source share
2 answers
  • why you pass the form DOM table.
  • Why are you looking for a class colCategory, why aren't you using class of input tag, whicheetag.
  • You put this code in a function $(document).ready().

Try it anyway.

$(document).ready(function(){ //by using td class
      $(".colCategory").each(function(){
            $(this).find('input').val("Phone");
      })
})

OR simple way:

$(document).ready(function(){ //by using input class
      $(".eetag").each(function(){
            $(this).val("Phone");
       })
})
+3
source

td colCategory, input.colCategory. , input's, colCategory

,

$("#tblTransactions > tbody  > tr > td.colCategory > input").each(function(){
    var elt = $(this).val("Phone");
});

, inputs td's, colCategory.

+2

All Articles