8

Imagine this:

<svg>
    <g id="node1" class="node"></g>
    <g id="node2" class="node"></g>
</svg>

How can I find the 'g' tag, I want that all tags could be clicked, and not just 'node1' or 'node2'. I've tried simular to this, but could not get it work.

$('g').click(function(){
    alert("Hellooooo");
});
Kungen
  • 607
  • 1
  • 11
  • 24

2 Answers2

11

Use find() method for more info visit this

For eg $("body").find("p").css("background-color","#f00"); sets all body's <p> element background-color to red.

For your question try this:

$("svg").find("g").click(function(){

// your jquery code here

}
);
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
2

Thanks to C-Link I've solved this.

To be sure only the nodes are clickable I've wroted:

$("svg").find("g.node").click(function(){
    alert("Lolol");
});

And it works fine.

Kungen
  • 607
  • 1
  • 11
  • 24
  • 1
    +1. Don't deserve a downvote as it is good to know the long way and short way. As explained above, in some cases, you'd need the long method. – Alvin K. Jun 13 '13 at 18:28