$(this) throws "undefined" but this works as intended

Seemax

Please, consider two pieces of code.

1) Works as intended:

  $(function(){
    $('.menu  li').on('click', function(){
    var choice = document.getElementById("choice");
    var text = this.textContent;
      choice.textContent = text;
    });
     });

2) In this case, $(this) throws "undefined".

$(function(){
$('.menu  li').on('click', function(){
var choice = document.getElementById("choice");
var text = $(this).textContent;
  choice.textContent = text;
});
 });

I've been using $(this) as a reference to selected element for a long period of time. But today it failed. What's wrong? Should I forget about $(this) and never more be facing such a case in a few lines of simple code?

Codepen

oriadam

The .textContent is a DOM property, not a jQuery property or method. This is why $(this), which is a jQuery element, does not have it.

You can use $(this)[0] to get the actual DOM property out of the jQuery element, like that:

var text = $(this)[0].textContent;

However $(this)[0] is equivalent to this so there's no point doing so in that specific example. It might make sense in other cases - for example, if you get a jQuery element as a function argument:

function set_text(jqElem,txt) { jqElem[0].textContent = txt; }

You can also use the jQuery method .text() to get or set the text content of the element, like that:

var text = $(this).text();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaScript Arrow Function throws Undefined

Spring throws exception for undefined bean

Angular filter works, but sees input as undefined and throws error

SuluArticleBundle throws undefined index: article

Why popFirst throws an error, but removeFirst works?

HttpGet throws 500 error on vuejs but works with postman

contract name throws undefined error?

Styling works as intended in 'responsive design mode' but breaks in small desktop window

Pascal's triangle works but throws an Notices

Tensorflow.js inconsistent prediction, returns 0 or works as intended

UISplitViewController in iOS 14 works as intended in landscape mode, but not in portrait

App Throws Fatal Error, then reboots and works

logout.php does not redirect to intended page on server but works on localhost

Slim redirect works; but also throws an exception

Undefined method `foo' for nil:NilClass — throws error in view but works in rails console

JQuery script works on Firefox but throws "undefined is not a function" in Chrome

Opening file throws error but still works

'$ is undefined' error but the function works

CSS animation fails first then works as intended

Javascript throws undefined

Gradle Jar works as intended, but Gradle Run fails

macos bash seems to ignore elif and only the first if statement works as intended

Query works fine,but SQLCommand throws 'invalid name'

Node.js throws TypeError: Cannot destructure property 'firstName' of 'req.body' as it is undefined, even though it works for an other post request

Make sure type constraint works as intended without regressions

Fade effect blinks first time it's triggered, but then works as intended

removeChild() works as intended but gives an error?

React app production version changes css so it no longer works as intended

How Can I Know If This Program Works As i Intended( Dynamic stack )