Why is 'getattribute()' not a function?

Asked

Viewed 155 times

3

I’m trying to access the attribute xlink:href of a <a> within an SVG. But when I try to access this attribute, Console Log returns that getAttribute() is not a function.

THE HTML:

<svg version="1.1" id="svg-map" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="450px" height="460px" viewBox="0 0 450 460" enable-background="new 0 0 450 460" xml:space="preserve">
    <g>
        <a xlink:href="#tocantins" id="BITCH!">
            <path stroke="#FFFFFF" stroke-width="1.0404" stroke-linecap="round" stroke-linejoin="round" d="M289.558,235.641
                c16.104,0.575,44.973-31.647,44.835-45.259c-0.136-13.612-17.227-58.446-22.349-66.088c-5.122-7.628-37.905,2.506-37.905,2.506
                S234.852,233.695,289.558,235.641z"></path>
            <text transform="matrix(1 0 0 1 287.0137 188.3208)" fill="#FFFFFF"></text>
            <image width="42" height="55" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACoAAAA3CAYAAAB6i9geAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDY3IDc5LjE1Nzc0NywgMjAxNS8wMy8zMC0yMzo0MDo0MiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmN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Javascript:

document.querySelector('#svg-map').addEventListener('mouseover', function(){
    var link = this.getElementsByTagName('g a');
    console.log('Hello World ' + link.getAttribute('id'));
});

What am I doing wrong?

3 answers

5


Because getElementsByTagName() returns a HTMLCollection (who behaves like a array), then you need to access as a array: link[0].getAttribute('id').

Another problem is that .getElementsByTagName('g a') is not valid, since this function accepts only the name of one tag, not several, the best solution would be to use .querySelector(), like you did in the first line.

document.querySelector('#svg-map').addEventListener('mouseover', function(){
    var link = this.querySelector('g a');
    console.log('Hello World ' + link.getAttribute('id'));
});

4

The method getElementsByTagName returns a list of elements and not a single element, so of this problem, in the array there is no function getAttribute.

Do so:

document.querySelector('#svg-map').addEventListener('mouseover', function(){
    var link = this.getElementsByTagName('g a')[0];
    if (link !== undefined) {
        console.log('Hello World ' + link.getAttribute('id'));
    }
});
  • I believe that getelementsbytagname('g a')[0] will not bring any return, should follow the guidance of @Sanction and querySelector

1

Try with attributes thus:

document.querySelector('#svg-map').addEventListener('mouseover', function(){
    var link = this.getElementsByTagName('a');
    console.log(link[0].attributes['xlink:href'].value);
    console.log('Hello World ' + link[0].id);
});

Exit:

#tocantins
Hello World BITCH!

See working on Jsfiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.