<table>
<tr>
<td>aaaa</td>
<td>bbbb</td>
</tr>
<tr>
<td></td>
<td>ccccc</td>
</tr>
</table>
For above table, how can i append a string “iString” to the cell data “aaaa”, “bbbb” and “cccc”. But not for the empty cell.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is a jsfiddle example.
The
$("table td")selector selects eachtdelement of the table, and the.each()is an iterator function, which will execute the callback provided as its argument for each element that matches the selector, which are thetdelements of the table. Inside the callback,thiswill refer to thetdelement. The –$(this).text().lengthportion is checking to see if the text inside that
tdelement has a length greater than zero. If it is, then you’ve got atdelement with a text. The –var text = $(this).text();line fetches that text from the
tdand assigns it to thetextvariable. The next line concatenates the specified string with this variable. Finally, the –$(this).text(text);line assigns the concatenated string as the
tdelement’s text.Edit
I’ve added the
$.trim()function around the string-length checking to remove the white-space characters. If you want to consider the white-spaces as characters, just remove it.