I want to select text
tag. I have tried different methods. This is a part of a bigger problem. I want to select the text tag and append it. there are multiple elements of tag I want the specific the bottom one and I cannot do any changes in SVG code as it is created by Google API So I have to do all changes by JS
var gElement = document.querySelector('#chart_div svg > g:nth-child(9)');
var svg = document.getElementById("chart_div").getElementsByTagName("svg")[0];
var eighthGText = svg.getElementsByTagName("g")[7].getElementsByTagName("text")[0];
<div id="chart_div">
<div>
<div>
<svg width="800" height="600">
<defs></defs>
<g></g>
<g> <text></text></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g>
<text></text>
</g></svg>
</div>
</div>
</div>
var svg = document.getElementById("chart_div").getElementsByTagName("svg")[0];
var eighthGText = svg.getElementsByTagName("g")[7].getElementsByTagName("text")[0];
var gElement = document.querySelector(‘#chart_div svg > g:nth-child(9)’);
2
Answers
You select it the same way you would any other element, eg.
document.querySelector('text')
.Here’s a working example. Note the use of
x
andy
attributes on thetext
element to ensure it’s actually visible – which may in fact be your actual problem.The fucntion TestIt() will display the text inside the specific text tag…