+ 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)
12 Antworten
+ 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)
+ 2
yes right, but can I have a code to better understand it please ? ^^'
+ 2
to remove objects it's done, but for the opacity I didn't understand how to do
+ 2
I don't want to reset, but to set
+ 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
+ 1
I'm using js class, so when I do el.remove(), in the method I must use "this" and not el or refEl
+ 1
oh I've an idea : you mean remove the element in the array, then redraw ?
+ 1
this.parentNode.removeChild(this)
--> parentNode is undefined
???
+ 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
0
You have always to have a reference to element... More details about the class would be helpful
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