As a newcomer to VueJS, I find myself struggling with some basic tasks. One particular challenge I'm facing is trying to manipulate an SVG element on my webpage, such as deleting the children of a <g>
element and replacing them with new ones. Despite numerous attempts, nothing seems to work and I am at a loss.
Let's take a look at a simplified version of the code...
HTML
<svg id="hz_svg" data-name="hz_svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1600 1000">
<g class="scale" width ="1400" style="fill: none; stroke: white; stroke-width: 2px;">
<line x1="100" y1="1000" x2="1500" y2="1000" />
<g class="ticks">
<line x1="100" y1="1000" x2="100" y2="980"/>
<line x1="800" y1="1000" x2="800" y2="980"/>
<line x1="1500" y1="1000" x2="1500" y2="980"/>
</g>
</g>
</svg>
App.js
const ticksEl = document.querySelector("#hz_svg .ticks");
createApp({
methods: {
renderTicks() {
ticksEl.replaceChildren();
console.log('REMOVED!!!');
},
The interesting part here is that even though console.log('REMOVED!!!')
is executed, indicating that ticksEl.replaceChildren()
ran successfully, the elements on the page remain unchanged. The svg <line>
s within ticks
are not removed. It's worth noting that this issue does not seem to be related to the JavaScript itself, as placing replaceChildren()
outside of createApp works perfectly fine for removing the elements.
So, why am I unable to remove these elements? What could be causing this unexpected behavior?
Just to clarify, I prefer not to store the line elements within the VueJS data unnecessarily.
Note: Upon running console.log(ticksEl.children)
after console.log('Removed!!!')
, it shows no lines (children), even though they are still visible on the page.