Reputation: 3313
This is related to a previous question I asked with Jquery. If I have multiple tables throughout a webpage as shown below. Is it possible to find the name after Edited by: and replace it with something else. The name will always be different and replaced with "anon". The suggested example I've been using is from my previous question works perfectly but in there I know the class and ID of my div, How would I do it with contents within the table element?
Many thanks
<script type="text/javascript">
$(document).ready(function() {
$('div.blogEntries div.entry span').remove();
$('.entryFooter').each(function() {
var html_arr = $(this).html().split(' ');
html_arr[2] = 'anonymous';
$(this).html(html_arr.join(' '));
});
});
</script>
<table border="0">
<tbody style="border-radius: 8px 8px 8px 8px;">
<tr>
<td>
An example post
<br>
Edited by: joe at 03/11/2011 14:09
<br>
<br>
</td>
</tr>
</tbody>
</table>
Upvotes: 0
Views: 3030
Reputation: 8720
I think you want a regex. Here's an alternative to Yuriy's answer:
var regex = /Edited by: ([\w]*)? at/g;
var tables = document.getElementsByTagName("table");
for (var i=0; i<tables.length; i++){
var str = tables[i].innerHTML;
console.log(str);
str = str.replace(regex, "Edited by: anon at");
tables[i].innerHTML = str;
}
Upvotes: 1
Reputation: 22448
You can use regular expression to replace html:
var replacementRegexp = /(.*Edited by:\s+)(.+)(\s+at.*)/m;
$("table td:contains('Edited by')").each(
function(index, element) {
$(element).html($(element).html().replace(replacementRegexp, '$1anon$3'));
});
Upvotes: 2
Reputation: 137
what about looping through the children of the table and search the text for the string "Edited by:". I cant think of the code of hand but something like:
$("#yourTableBody").children().each(function(){
if( $(this).text().search("Edited by") > -1 )
{
//edit the string here
}
});
Upvotes: 0
Reputation: 6441
You need more HTML elements:
<span class="editedby">Edited by: <strong>joe</strong> at <em>03/11/2011 14:09<em><span>
$('span.editedby strong').each( function () {
$(this).text('anon');
} );
Upvotes: 0