Jump to content

[SOLVED] Get text lines


julia k

Recommended Posts

Hi! I have another question:

 

Let's say I have this text block:

Lorem ipsum dolor sit amet, consectetur adipisicing elit
sed do eiusmod tempor incididunt ut labore et dolore...
Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.

and after I select it how can I display each line of text in an alert window?

Link to comment
Share on other sites

Hi Julie,

 

All in one alert box? Try something like this:

<script type="text/javascript">
alert("Lorem ipsum dolor sit amet, consectetur adipisicing elit \r\nsed do eiusmod tempo \r\nincididunt ut labore et dolore... \r\nUt enim ad minim veniam, quis nostrud exercitation \r\nullamco laboris nisi ut aliquip ex ea commodo consequat.");
</script>

 

Note the \r\n - this means "return new line". Is this what you're looking for?

Link to comment
Share on other sites

ohh noes..not like that! :)

 

I'm looking for a way to detect when the lines are broken and display each line in an alert box, something like, if the selected text has 5 lines then 5 alerts will be displayed, one for each line.

 

I have a function that detects the selected text but I don't know how to get a reference to each of the lines in that text block.

Maybe using regex? but I'm not good at that.. :(

Link to comment
Share on other sites

This is the function I'm using to add different html tags to a simple text editor:

/*
*   Convenience function to inject markup
*
* Access: private
*/
TextEditor.prototype.private_addTags = function(tagStart, tagEnd)
{
    var textarea = this.editor;

    // Code for IE
    if (document.selection) 
    {
        textarea.focus();
        var sel = document.selection.createRange();
        sel.text = tagStart + sel.text + tagEnd;
    }
    else 
    {  // Code for Firefox
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;

        var sel = textarea.value.substring(start, end);
        var rep = tagStart + sel + tagEnd;
        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
    }
};

 

My target is to be able to create lists from selected text, that's why I need a reference to each of the lines in a text block. For each line found I'll use the above function to create list items.

 

can you help, please? :)

Link to comment
Share on other sites

oh man.. I must be really tired for not seeing this earlier...

 

this is my test page and my solution, if you want to give it a try :)

 

<!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></title>
</head>
<body>
<textarea id="content" rows="20" cols="100"></textarea>

<input type="button" id="btn" value="get text" />

<script type="text/javascript">
var getSelection = function(textarea)
{
    // Code for IE
    if (document.selection) 
    {
        textarea.focus();
        var sel = document.selection.createRange();
        return sel;
    }
    else 
    {  // Code for Mozilla Firefox
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;

        var sel = textarea.value.substring(start, end);
        return sel;
    }
};

var area = document.getElementById('content');

document.getElementById('btn').onclick = function()
{
    var text = getSelection(area);
    var newtext = text.split(" ");
    for(var i=0;i<newtext.length;i++)
    {
        alert(newtext);
    }
    
};

</script>

</body>
</html>

 

all I had to do was to search for the line breaks (  )

 

easy peasy :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.