Error on converting Jquery val().split() to Vanilla Javascript

In the process of converting my code from Jquery to Vanilla JS I have this code snippet that I can’t convert:

function updateTextBox(nehemyah) {
  var delfino = $("#campotxt").val().split("\n");
  delfino.remove(nehemyah);
  $("#campotxt").val(delfino.join("\n"));
}

I tried this:

let camPito = document.getElementById("campotxt");
function updateTextBox(nehemyah) {
    let delfino = camPito.value;
    delfino = delfino.split("\n");
    delfino = delfino.remove(nehemyah);
    delfino = delfino.join("\n");
  }

The idea is that this textarea is cleared line per line once I press a button. If I use that Jquery snippet it works without problem.

>Solution :

Array.prototype.remove() is not a standard method, so I guess you’ve defined that elsewhere in your code. It has nothing to do with jQuery.

Since this line isn’t jQuery, there’s no reason to change it. Presumably it doesn’t return the array, it just modifies it in place, so you shouldn’t assign the result back to the variable.

You’re not assigning the final result back to the value of the input.

function updateTextBox(nehemyah) {
  let camPito = document.getElementById("campotxt");

  let delfino = camPito.value.split("\n");
  delfino.remove(nehemyah);
  camPito.value = delfino.join("\n");
}

Leave a Reply