Jump to content

Delete last line


dotkpay

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.