Jump to content

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

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.