0

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

}

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.