javascript match of new line in IE not working

the situation is as follows.. I ready a value from a mysql field using PHP.. Dispays that value in a HTML div and i'm reading the div's value:

value = document.getElementById("text").innerHTML;
            
if (value.match(/\n/))
{
   alert("new line detected");
}
else
{
   alert("no new lines");
}

but it seems to work only in FF..

IE6/7 doesn't detects the new lines however in the source view of the both browsers the text is displayed on new lines..

any ideas how to fix that ?
LVL 1
SteeleRBGAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteeleRBGAuthor Commented:
just let me clear it out.. adding nl2br in the PHP won't solve the problem.. i need to keep the HTML format in tacked as is in the MySQL field..
LordOfPortsCommented:
In IE the innerHTML member method of the div object appears to replace the new line \n with a space. I wrote the code below that outputs each character along with the character code, now in Firefox the innerHMTL function returns the new line, character code 10, but in IE it is replace with a space, character code 32. I am not sure of any solution to this behavior. If you cannot use nl2br in your PHP perhaps you could replace new line \n with 2 or 3 consecutive spaces which will be only displayed as one space in your HTML by default and then when you test the content of the div text using innerHTML you could test for presence of the 2 or 3 spaces.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script type="text/javascript">
    
    function CheckNewLine(divID)
    {
        var nNewLineCode = 10; // Character code for new line
        var bHasNewLine = false;
 
        var sText = document.getElementById(divID).innerHTML;
        
        for(var i = 0; i < sText.length; i++) {
            //alert(sText.charCodeAt(i));
            document.write(sText.substring(i, i + 1) + "<sup><span style=\"color:#CCCCCC;\">" + sText.charCodeAt(i) + "</span></sup>");
            if(parseInt(sText.charCodeAt(i)) == nNewLineCode) {
                bHasNewLine = true;
            }
        }
        
        if(bHasNewLine) {
            alert("FOUND new line.");
        }
        else {
            alert("DID NOT FIND new line.");
        }
    }
    
    </script>
</head>
<body>
<div id="divContent">Test sentence followed by new line.
Test sentence preceeded by new line.</div>
<input type="button" value="Check DIV above for New Line" onclick="CheckNewLine('divContent');" />
 
 
<div id="divContent2">Test sentence without new line.</div>
<input type="button" value="Check DIV above for New Line" onclick="CheckNewLine('divContent2');" />
</body>
</html>

Open in new window

FF.png
IE.png
SteeleRBGAuthor Commented:
can i ready the content of a div with anything else than innerHTML that may preserve the new line ?
LordOfPortsCommented:
Unfortunately innerText (IE only) behaves the same way, I do not any solution with a div. Could you use <textarea>? Using CSS you could make it appear the same as a DIV and its .value property will return a new line \n correctly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteeleRBGAuthor Commented:
well.. the whole idea is to use textarea only if there is a new line else to use the INPUT.. but i can try it out cause the textarea got the option to display only one row..

Thanks you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.