Home > Enterprise >  jQuery get content of textarea
jQuery get content of textarea

Time:10-13

I would like to get the content of my textarea when onkeyup event triggered. I tried this:

<textarea onkeyup="getContent(this)"></textarea>


function getContent(txtBeschreibung) {
   console.log(
    txtBeschreibung.val()
   )
}

but my output is this:

TypeError: txtBeschreibung.val is not a function. (In 'txtBeschreibung.val()', 'txtBeschreibung.val' is undefined)

Where is my mistake? :/

CodePudding user response:

I was able to get your code to run with minor modifications

function getContent(txtBeschreibung) {
   let val = $(txtBeschreibung).val();
   console.log(val);
}

It looks like you need to wrap the reference (this) in a dollar-sign to perform jQuery operations on it.

CodePudding user response:

txtBeschreibung isn't a jQuery object, to use jQuery, you could do this

function getContent(txtBeschreibung) {
   console.log($(txtBeschreibung).val());
}

or in Vanilla JS:

function getContent(txtBeschreibung) {
   console.log(txtBeschreibung.value);
}
  • Related