1

I have a container that opens via an onclick function. I then have a cross within the container that should close the parent element however I receive a

TypeError: undefined is not an object (evaluating 'parent.id')

Code is here

<div class="post" onclick="postClick(el)">
...
...
</div>

JavaScript

             function postClick(el) {
                     document.getElementById(el.id).classList.add("read");
             }
             function postClose(event) {
                 var parent = this.parentNode;
                 console.log(parent.id);
                 parent.id.classList.remove("read");         
             }
2
  • What does the console.log(parent.id); return? Commented Aug 4, 2020 at 21:16
  • @AndrewL64 undefined Commented Aug 4, 2020 at 21:19

4 Answers 4

4

Use event.target to get the reference to the HTML element. And you have an extra .id in the parent.id.classList expression.

function postClick(event) {
  const el = event.target;
  document.getElementById(el.id).classList.add("read");
}

function postClose(event) {
  const el = event.target;
  const parent = el.parentNode;
  console.log(parent.id);
  parent.classList.remove("read");         
 }
<div class="post" onclick="postClick(event)">
...
...
</div>

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

3 Comments

TypeError: undefined is not an object (evaluating 'event.target')
Your problem is related to extra id in parent.id.classList.remove ;)
I have parent.classList.remove("read"); as the last line and it is still returning TypeError: undefined is not an object (evaluating 'event.target')
0

One way of doing this is using pure Javascript and bind the event listener like this

document.querySelector('#toggle').addEventListener('click', function (e) {
  console.log(this.parentNode.classList.remove('read'))
});
div {
  padding: 20px 50px;
}

div.read {
  background-color: red;
}
<div class="read">
  <button id="toggle">Remove Parent Class</button>
</div>

1 Comment

If parent.id returns undefined then the parent probably has no id tag
0

Jut use this and you are done : 😊

element.parentNode.classList.remove("class-name");

Comments

-3

if the project is complex and needs interactivity more than often then you use jquery library for the interactivity.

//to remove class
$( "p" ).removeClass( "myClass yourClass" )


$("#div123").toggle(); //if you want to temp hide elements

as your code suggests the 'read' items must be disabled, you can toggle them once an event handler is wrapped over the toggle method. you can pass this or $(this) in case you want to do stuff with the owner of the function call.

well i agree some adept devs didnt like this answer, it will be surely of some help to some beginner dev in future who is looking for an alternative option to hide elements or remove classes

3 Comments

Please don't ever again recommend jQuery (which is a huge library) for such a simple task.
I only have like 10 javascript functions on my whole site, I don't see a need for JQuery
@mac my answer explains "Remove class from parent element javascript" adequately. it was intended to save developer time and reduce maintenance. if you are really comfy with native JS then its upto you.

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.