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
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.
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.
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.
DOM
form table
. colCategory
class why don't you use class of input tag
that is eetag.
$(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");
})
})
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With