1

I'm trying to retrieve the value of svg g element. Below is my code

<div id="chart">
    <svg width="1386" height="186">
    <g transform="translate(0,0)">    
    <g><circle class="node highlight" r="17.603478526018918" style="fill: #008000;" cx="521.2058097619438" cy="67.43023189750437"></circle>
    </g>
    <g><text class="label highlight" dx="22.603478526018918" dy=".35em" transform="translate(521.2058097619438,67.43023189750437)">CNN</text>
    </g>
    </g>
    </svg>
</div>

I want to access the third 'g' element value i.e. 'CNN' in above case. How can I do it using jQuery.

Subash Basnet
  • 307
  • 1
  • 4
  • 15

2 Answers2

0

Try this:

console.log($('#chart').find('g').eq(2).text());

or this way:

console.log($('#chart').find('text').eq(0).text());

even a better one:

$('#chart g').find('text').text();
Jai
  • 74,255
  • 12
  • 74
  • 103
0
s = '<div id="chart"><svg width="1386" height="186"><g transform="translate(0,0)"><g><circle class="node highlight" r="17.603478526018918" style="fill: #008000;" cx="521.2058097619438" cy="67.43023189750437"></circle></g><g><text class="label highlight" dx="22.603478526018918" dy=".35em" transform="translate(521.2058097619438,67.43023189750437)">CNN</text></g></g></svg></div>'

$('g:nth-of-type(2)',$(s))
alexg
  • 3,015
  • 3
  • 23
  • 36