julia k Posted January 5, 2009 Share Posted January 5, 2009 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? Quote Link to comment Share on other sites More sharing options...
shlumph Posted January 6, 2009 Share Posted January 6, 2009 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? Quote Link to comment Share on other sites More sharing options...
julia k Posted January 6, 2009 Author Share Posted January 6, 2009 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.. Quote Link to comment Share on other sites More sharing options...
julia k Posted January 6, 2009 Author Share Posted January 6, 2009 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? Quote Link to comment Share on other sites More sharing options...
julia k Posted January 6, 2009 Author Share Posted January 6, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.