Reputation: 2386
I found this issue to always happen and not consistent.
This issue causing jquery .text comparison not working, because jQuery trying to compare actual wording but fail.
Eg: 'Second Title' is showing as 1 line of wording at browser, but when view html source, the 'Second' and 'Title' is in different line.
Thus, causing if($(this).text()=="Second Title")
not working.
Any good way to eliminate the whitespaces at html source in such case? so that I can continue to use .text comparison.
Sample showing not working version.
<table border="0" cellpadding="0" cellspacing="1">
<tr>
<td width="400" colspan="3" align="center"><span>First Title</span></td>
</tr>
<tr align="center">
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td width="400" colspan="3" align="center"><span>Second
Title</span></td>
</tr>
<tr align="center">
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
$(document).ready(function() {
replaceText();
});
function replaceText()
{
var first, second
first= "", second="";
$("*").each(function() {
if($(this).children().length==0)
{
if($(this).text()=="First Title")
{
first+= jQuery.trim($(this).closest('tr').next('tr').find("td").eq(0).text());
}
if($(this).text()=="Second Title")
{
second+= jQuery.trim($(this).closest('tr').next('tr').find("td").eq(0).text());
}
}
});
alert(first);
alert(second); //fail
}
Upvotes: 0
Views: 204
Reputation: 12214
If you don't rely on repetitions of whitespace (say, 3 spaces), you could replace all contiguous whitespace by a space.
function shrinkWhitespace(t) {
return t.replace(/\s+/g, ' ');
}
Example
>> shrinkWhitespace('Second \n\tTitle') == 'Second Title'
true
Upvotes: 1