Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
jQuery: select one specific element from clicked SVG group

So, I have a simple SVG group:

<g id="interceptor">
    <ellipse cx="0" cy="15" rx="6" ry="14" fill="url(#gradient)"/>
    <polygon points="0,-20 7,10 0,0 -7,10" fill="blue"/>
    <polygon points="0,-20 7,10 0,0" fill="darkblue"/>
</g>

Which I then use like this:

<g class="ship" id="ship3" transform="rotate(-180,480,24) translate(480,24)">
   <use xlink:href="#interceptor"/>
</g>

Now, what I would like to do is to add a stroke around the first polygon from the group when the whole group is clicked, but I don't know how to access contents of the interceptor group via jQuery. I was thinking about something like this, but contents() returns nothing:

$(".ship").click(function() {
    $(this).find("use").contents().find("polygon").first().attr({"stroke-width":1, stroke:"white"});
});

I can, of course, add a stroke around the entire ".ship" and it works, but it's not what I want because this creates strokes between polygons in the group. Additionally, there can be a number of separate objects that use the #interceptor group, and only the one that is actually clicked should get the stroke. Is that doable?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!