Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:14:04+00:00 2026-06-11T14:14:04+00:00

I would like to create a web page displaying an interactive svg: since several

  • 0

I would like to create a web page displaying an interactive svg: since several svg may be used, the various objects displayed will have different IDs, so the event listeners (to catch a mouse click, for example) have to be dynamic.

Starting from this snippet

var a = document.getElementById("alphasvg");
a.addEventListener("load",function(){
        var svgDoc = a.contentDocument;
        var delta = svgDoc.getElementById("delta");
        delta.addEventListener("click",function(){alert('hello world!')},false);
    },false);

I would like to find a way to cycle through all the objects of the svg (maybe having a particular class) and attach an even listener to them.

update

So JQuery ‘each’ function may be a suitable option, but it seems that JQuery doesn’t handle the svg DOM so well. Is there any other available option? (like a JQuery plugin?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:14:05+00:00Added an answer on June 11, 2026 at 2:14 pm

    This is my preferred structure for adding event listeners to SVG elements with vanilla js…

    // select elements
    var elements = Array.from(document.querySelectorAll('svg .selector'));
    
    // add event listeners
    elements.forEach(function(el) {
       el.addEventListener("touchstart", start);
       el.addEventListener("mousedown",  start);
       el.addEventListener("touchmove",  move);
       el.addEventListener("mousemove",  move);
    })
    
    // event listener functions
    
    function start(e){
      console.log(e);
      // just an example
    }
    
    function move(e){
      console.log(e);
      // just an example
    }
    

    The sample code you present is somewhat contrived, but here’s a rewrite that makes it work…

    var a = document.getElementById("alphasvg");
    a.addEventListener("load",function(){
      var svgDoc = a.contentDocument;
      var els = svgDoc.querySelectorAll(".myclass");
      for (var i = 0, length = els.length; i < length; i++) {
        els[i].addEventListener("click", 
           function(){ console.log("clicked"); 
        }, false);
      }
    },false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a flash presentation for a web page. It would
Is it possible/easy to create a web page which would act like a Microsoft
I would like to create web page which will print simple message x minutes
I would like very much to create a web page which would have the
I would like to create web page which has to compatible on iPhone.. i
I would like to create a System Health web page which will monitor the
I would like to toggle (hide/show) several elements of my web page. All those
I would like to know if there is a way to create web pages
I would like to create a configuration panel for the homepage of the web-app
Possible Duplicate: Create event handler for OnScroll for web browser control I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.