JS Vanilla - remove() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

JS Vanilla - remove()

How can I remove element, but in this case : el = new myClass(); el.remove(); //// class class myClass { constructor() { ... } remove() { // here } } same question to modify style of this (this.style.opacity doesn't work)

13th Feb 2019, 9:55 AM
NoxFly
NoxFly - avatar
12 Answers
+ 3
Another problem is: How identify which element remove? Suppose than you have: let svg= new SVG(...) // here some code than add svg childs // What has to remove it?? svg.remove() A solution is use an index (which identify the order of inserted childs) but is not really practical and give you futher problem when you have to insert nested childs (child in child). Another solution would be return to any created child some "id" than you SVG object can understand, and use it for deletion like: let svg= new SVG(..) let circle1= svg.circle(...) let circle2= svg.circle(...) .... svg.remove(circle1)
13th Feb 2019, 12:30 PM
KrOW
KrOW - avatar
+ 2
yes right, but can I have a code to better understand it please ? ^^'
13th Feb 2019, 11:26 AM
NoxFly
NoxFly - avatar
+ 2
to remove objects it's done, but for the opacity I didn't understand how to do
13th Feb 2019, 1:43 PM
NoxFly
NoxFly - avatar
+ 2
I don't want to reset, but to set
13th Feb 2019, 2:00 PM
NoxFly
NoxFly - avatar
+ 1
I dont fully understood your question... You want remove an element by dom tree using JS? If yes you can do it like: refEl.parentNode.removeChild(refEl) For a style prop you can reset it by assigning null... A simple code here https://code.sololearn.com/WJ9wYKgmPLY0/?ref=app
13th Feb 2019, 10:24 AM
KrOW
KrOW - avatar
+ 1
I'm using js class, so when I do el.remove(), in the method I must use "this" and not el or refEl
13th Feb 2019, 10:26 AM
NoxFly
NoxFly - avatar
+ 1
oh I've an idea : you mean remove the element in the array, then redraw ?
13th Feb 2019, 11:27 AM
NoxFly
NoxFly - avatar
+ 1
this.parentNode.removeChild(this) --> parentNode is undefined ???
13th Feb 2019, 11:31 AM
NoxFly
NoxFly - avatar
+ 1
If you want reset a style prop setted with el.style.PROP just set it to null... Example: el.style.fill= 'red'; .... el.style.fill= null
13th Feb 2019, 1:54 PM
KrOW
KrOW - avatar
0
You have always to have a reference to element... More details about the class would be helpful
13th Feb 2019, 10:28 AM
KrOW
KrOW - avatar
0
Your code seem generating first a series of html representing svg childs inserted in dom when draw is called, right? If it work in this way, remove() have to remove the data from data array and from injected svg element (.name) which you have to get the reference (using querySelector all et similar) and procede like i said previously
13th Feb 2019, 10:40 AM
KrOW
KrOW - avatar