dotkpay Posted December 17, 2011 Share Posted December 17, 2011 Hello, Am writing an html/js script with a textarea but I need a button which deletes only the contents of the last line. I guess the script should look something like this: <html> <head> <script type="text/javascript"> function delete() { //contents to appear here; } </script> </head> <body> <textarea cols="50" rows="35" id="area"></textarea> <button onclick="delete()">Delete Last Line</button> </body> </html> Link to comment https://forums.phpfreaks.com/topic/253365-delete-last-line/ Share on other sites More sharing options...
joe92 Posted December 17, 2011 Share Posted December 17, 2011 This would be tricky, but I think regex could possibly help you here. Try searching for something the begins with a variation \r\n and is at the end of the textarea, then replace it with a string consisting of nothing. Give this a try; function delete(){ var textarea = document.getElementById('area'); var string = teatarea.value; var new_string = string.replace(/\r?\n.?*$/, ''); textarea.value = new_string; } This is untested but should be a good starting point, Joe Link to comment https://forums.phpfreaks.com/topic/253365-delete-last-line/#findComment-1298800 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.