Remove every even line from textarea using jQuery

Rajiv Srivastava

I am having a textbox with some values in it. Now I want to remove every even line value from the textarea on click of a button. Please look at my code and suggest me how can I do it correctly.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<textarea id="dinput">
10.3    137
10.9    394 <!--remove this-->
11.5    847
12.2    394 <!--remove this-->
12.9    930
13.4    940 <!--remove this-->
14.1    368
14.8    849 <!--remove this-->
15.4    563
</textarea>
<input type="button" value="Filter Data" class="filterdata">
<script>
$(".filterdata").click(function(){
  const txtar = $('#dinput').val();
  txtar.find(":even").remove();
  $('#dinput').val(txtar.html());
});
</script>

Dimitris Maragkos

Split the textarea value to an array of strings using newline character (\n) as separator. Remove odd indexes from array using Array.filter() function. Re-join array elements to string and set as textarea value.

$(".filterdata").click(function() {
  const txtar = $('#dinput').val();

  let lines = txtar.split('\n');

  let evenLines = lines.filter(function(v, i) {
    // check the index is odd
    return i % 2 == 0;
  });

  $('#dinput').val(evenLines.join('\n'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<textarea id="dinput">
10.3    137
10.9    394 <!--remove this-->
11.5    847
12.2    394 <!--remove this-->
12.9    930
13.4    940 <!--remove this-->
14.1    368
14.8    849 <!--remove this-->
15.4    563
</textarea>
<input type="button" value="Filter Data" class="filterdata">

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove first line from Textarea

Onclick Add/Remove specific string from textarea using jQuery

remove certain custom html from textarea using jQuery

Remove characters from every line of a file in shell

How can i remove all characters started with # from a textarea using jquery or javascript?

jQuery Textarea - Remove spacing but add line breaks after each word

How to remove character from every class in jQuery?

How to add a character after every ODD line and before every EVEN line using Notepad++

JavaScript - How to remove new line from textarea if no text exists before?

Possible Paste From Textarea into another textarea input using button? | jQuery

jQuery each line in textarea

Remove a string in every line

remove backslash at every line

How to remove all the duplicated words on every line using Notepad++?

Remove everything in every line after/before the mentioned text using JavaScript

Remove every line before a string in file using Ansible

Remove scrollbars from textarea

StringJoiner remove delimeter from first position for every line

remove the first 15 characters from every other line in a file

How to remove the first dot from every line but only if it exists

Javascript remove spaces in the beginning of every line from innerHTML

Remove everything except some word from every line

How to remove every line starting with specific string from log file?

removing line by line from textarea

How can I insert a lot of records into database all at once from a textarea where every line of it is a record?

jQuery: Remove duplicate string on textarea

Multiple line replace jQuery textarea

remove line comment from file by using regex

Remove line from php file using sed

TOP Ranking

HotTag

Archive