Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery set value of all inputs inside a certain td

Tags:

jquery

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 the page loads I want to set the value for each input for every td with the class "colCategory".

I would like the selector to look something like this but am having troubles with the syntax...

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

Could someone help me with this syntax please. Or if there is a simpler way of doing it I'm open to suggestions.

thx

like image 518
Richie Avatar asked Aug 09 '14 06:08

Richie


People also ask

How do I set the value of a element in jQuery?

The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.

How to take value of input in jQuery?

jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element.

How to get and set the values of HTML form fields in jQuery?

To get a form value with jQuery, you need to use the val() function. To set a form value with jQuery, you need to use the val() function, but to pass it a new value.


2 Answers

  1. why you are traversing the DOM form table.
  2. why you are looking onto colCategory class why don't you use class of input tag that is eetag.
  3. Are you placed this code into $(document).ready() function.

Any way Try this one.

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

OR simple way is:

$(document).ready(function(){ //by using input class
      $(".eetag").each(function(){
            $(this).val("Phone");
       })
})
like image 104
chandu Avatar answered Sep 22 '22 15:09

chandu


Your td has class colCategory but in selector you are specifying input.colCategory. So it is searching for all input's that has class colCategory

You should say like bellow

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

So here it will search inputs in td's which have class colCategory.

like image 30
Mritunjay Avatar answered Sep 23 '22 15:09

Mritunjay