Access an element by class name from SVG using JavaScript

vijay pawar

I am trying to access <g> tag from SVG by class name (ads) and which is rendered using JavaScript into <object> tag of HTML.

SVG is rendered here

<object id="container2" type="type=" image/svg+xml"></object>

Using Javascript

player2 = lottie.loadAnimation({
            container: document.getElementById('container2'),
            renderer: 'svg',
            loop: false,
            autoplay: true,
            path: 'assets/mindmap2.json'
        });

But the issue is When this function called, it gets <g> but in the console, it shows the zero elements inside the HTMLCollection.

 window.addEventListener("load", () => {
        console.log(document.getElementsByClassName('ads'));
    });

Microsoft Edge Console Log

Screenshot Image

vqf

Your javascript function gets called right after loading the window. At that time, the svg picture has not been loaded, and its inner elements do not exist. According to this answer, you might find the elements by changing the script to:

const el = document.getElementById('container2');
el.addEventListener("load", () => {
    console.log(document.getElementsByClassName('ads'));
});

The script should be called after loading #container2. You can just add the script after the html code or run the script after loading the page.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find class name of SVG element

Access class name in a window element

Unable to change class name of SVG element

How to extract "id" element from SVG file using Javascript?

How to check if an HTMLCollection contains an element with a given class name using javascript?

Getting text from an element with the same class name using jQuery

Getting text inside of an element from the class name using puppeteer

Inject SVG element and access it using jquery

access the 'd' element from an SVG object with casperjs

How to access element id from another page using JavaScript?

How to access a particular element from JSTL ForEach tag using javaScript?

How can I access an SVG element embedded in a CSS `background` from JavaScript?

Access Class Name From a Class Method

access jade element using javascript

How to access exported TypeScript class from JavaScript using AMD and RequireJS

How to access the second element that has the same class name in selenium using java

How to set different values on same name data attribute using values from select element with same class name

Class name added to an element using Jquery addClass is not detected when called using javascript onclick

How to access the .class object in java without using the class name from a static context

How to access variable from another class directly(i.e. without using class name or object)?

How to access the class itself from within a class without using its name

The class of the element change using the javascript

Get nearest element in SVG using JavaScript

Position element at center of svg viewbox using javascript

Vanilla Javascript - add Class-name to an element

How do I use the generated JsonProperty Name = name from JSON file to access a single element in a corresponding C# Class

Access SVG icons from javascript object

access SVG from Javascript does not work

Using Selenium to Find Element In an Array By Class Name