Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

equiv of ASP chr(13) ?

Status
Not open for further replies.

irate

Programmer
Jan 29, 2001
92
GB
I have a textarea that I am selecting text from to edit with a text range, i want to find the returns/new lines and add a list tag:

Code:
var textSelected = document.all.editbox.document.selection.createRange()
var addList = textSelected.text;
addList.replace(***,'<li>')

Where the *** is in the above code is where I need to detect teh returns/line feeds.

I have tried using \n and also String.fromCharCode(13) but neither works.

If i were using ASP i would use chr(13), is there and equivilent function in javascript?
 
if i remember \n (linefeed) is the equivalent of a chr(13) & chr(10)




 
yes i have tried the following:
Code:
addList.replace(\n,'<li>')
addList.replace('\n','<li>')
addList.replace(\r,'<li>')
addList.replace('\r','<li>')
addList.replace('\r\n','<li>')
addList.replace('\n\r','<li>')
addList.replace(String.fromCharCode(13),'<li>')
addList.replace(String.fromCharCode(10),'<li>')
addList.replace(String.fromCharCode(13)+String.fromCharCode(10),'<li>')
addList.replace(String.fromCharCode(10)+String.fromCharCode(13),'<li>')

None work, I have even made sure that the meta tag with the charset is on the page...?
I can't detect returns or new line feeds from my textarea!
 
this work :

<html>
<head>
<script language=javascript>
function fTest()
{
var szString = new String();
var re = new RegExp();
re = /\n/g

szString = (document.getElementById(&quot;TE1&quot;).value).replace(re ,&quot;<li>&quot;);

document.getElementById(&quot;TXT&quot;).innerHTML = &quot;<menu>&quot;+ szString + &quot;</menu>&quot;;
}

</script>
</head>
<body>

<textarea cols=50 rows=5 id=&quot;TE1&quot;></textarea>

<br><br>

<input type=button onClick=&quot;fTest()&quot; value=test>

<br><br><br>

<span id=TXT></span>



</body>
</html>
 
Yes that does work for the entire textarea, but, I am using the a selection of text from the mouse. This is called a text range, and then i take the text from the text range and put that into a variable... which means it doesn't work anymore, so i assume that the textarea alters the line feed somehow...

Code:
<html>
<head>
<script language=javascript>
function fTest()
{
    var textSelected = document.all.editbox.document.selection.createRange()

    var szString = new String();
    var re = new RegExp();
    re = /\n/g

    szString = textSelected.text
    szString.replace(re ,&quot;<li>&quot;);

    document.getElementById(&quot;TXT&quot;).innerHTML = &quot;<menu>&quot;+ szString + &quot;</menu>&quot;;
}

</script>
</head>
<body>

<textarea cols=50 rows=5 id=&quot;TE1&quot; name=&quot;editbox&quot;></textarea>

<br><br>

<input type=button onClick=&quot;fTest()&quot; value=test>

<br><br><br>

<span id=TXT></span>



</body>
</html>

 
Dont worry anymore, I have fixed it.

Here is the code fi you want to test it out, although I think the text range object only works in IE

Code:
<html>
 <head>
  <script>
   function listSelected1()
    {
     var textSelected = document.all.editbox.document.selection.createRange()
     if (textSelected.text != '')
      {
       var addList = textSelected.text;
       newLineFeeds = new RegExp();
       newLineFeeds = /(\r\n)/g;
       addList = addList.replace(newLineFeeds,'\n<li>');
       document.getElementById(&quot;preview&quot;).innerHTML = '<ul>\n<li>'+addList+'\n</ul>';
      }
    }
   function listSelected2()
    {
     newLineFeeds = new RegExp();
     newLineFeeds = /(\r\n)/g;
     var textSelected = (document.getElementById(&quot;editbox&quot;).value).replace(newLineFeeds,&quot;\n<li>&quot;);
     document.getElementById(&quot;preview&quot;).innerHTML = &quot;<ul>\n<li>&quot;+textSelected+&quot;\n</ul>&quot;;
    }
  </script>
 </head>
 <body>
  <textarea cols=&quot;50&quot; rows=&quot;5&quot; name=&quot;editbox&quot; id=&quot;editbox&quot;></textarea>
  <hr>
  <input type=&quot;button&quot; onClick=&quot;listSelected1()&quot; value=&quot;make list using mouse selection&quot;>
  <input type=&quot;button&quot; onClick=&quot;listSelected2()&quot; value=&quot;make list using entire textarea&quot;>
  <hr>
  <span id=&quot;preview&quot;></span>
 </body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top