1

In this function:

function myFunction (el) {
  if(!el) return;
  //input <el> is DOM
  var $el = $(el),
      dataObject = $el.data('elementId');
  //code here
}

I have this error:

"Uncaught TypeError: Cannot read property '$a' of null" in line dataObject = $el.data('elementId');

When I comment the line

if(!el) return; 

the error becomes

"Uncaught TypeError: Cannot read property '_a' of null"

I'm using jQuery 3.2.1

How can I fix this?

4
  • Can you post an example in which this occurs? Commented Mar 23, 2018 at 2:22
  • We don't see where you used $a so I don't know how can we help you... Commented Mar 23, 2018 at 2:33
  • @StupidKid that's most likely a variable within jquery. Commented Mar 23, 2018 at 2:35
  • I used Rollbar report when bug. I cannot build case like this >"< Commented Mar 23, 2018 at 2:37

1 Answer 1

0

function a(el) {
  if(!el) return;
  //input <el> is DOM
  var $el = $(el),
      dataObject = $el.data('elementid');
  //code here
  console.log(dataObject);
  console.log($el.attr('data-elementid'));
}
a(document.getElementById('two'));
a();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="two" data-elementid="a">2</div>

Try using attr instead of data? But by this answers:

this

if you set all to lowercase it will return data value.

Sign up to request clarification or add additional context in comments.

5 Comments

hmmm I don't think set all to lowercase is solution in this problem, and I can't set all attributes in my program, Ty
I need to see your program whole code but this one part I can't know what are your problems. I just copied your code and when you run it here on my answer it returns value from requested data..
@StupidKid the problem is that $el is sometimes null, which is upstream of anything that happens with .data.
@CertainPerformance yep, I think so, when I comment line if(!el) return; Error like this "Uncaught TypeError: Cannot read property '_a' of null". When I check is_null(el), Error like "Uncaught TypeError: Cannot read property '$a' of null" I think problem in jQuery but I haven't solution to fix it. because sometime error's appears
@user3739680, so, a simple enough solution would be to var $el = $(el); if (!$el) return; Or you could avoid jquery entirely, it's not necessary for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.