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 8630797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:07:33+00:00 2026-06-12T09:07:33+00:00

I am trying to dynamically wrap certain svg elements like rect , text ,etc

  • 0

I am trying to dynamically wrap certain svg elements like rect,text,etc into a single g using javascript (jQuery)

This is how the svg looks initially

<div id="container">
   <svg ...>
     <rect .../>
     <circle .../>
     <text ...>...</text>
   </svg>
</div>

The script (based on the helpful answers I received @ Inserting a (g) node in the middle of a tree (SVG) using jQuery) that I use to wrap into the g tag.

$("#container svg > *").wrapAll('<g id="parent" />');

The transformed svg looks like this

<div id="container">
   <g id="parent">
     <svg ...>
       <rect .../>
       <circle .../>
       <text ...>...</text>
     </svg>
   </g>
</div>

But nothing shows up on the UI. Even firebug shows the g to be grayed out (like it does for hidden elements).

enter image description here

calling $("#parent").show(); works in sometime cases but not all

Questions:

  • Why does the g that is dynamically created, hidden by default?
  • Why does $("#parent").show() work inconsistently?
  • Is there another (better) way of grouping grouping elements dynamically?
  • I am totally new to SVG, but relatively comfortable with jQuery and DOM manipulation. Is the way that I am treating SVG as just-another-tag wrong?

Tried on both Firefox (15.0.1) and Chrome (21.0.1180.89) with same results

  • 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-12T09:07:33+00:00Added an answer on June 12, 2026 at 9:07 am

    I believe the reason is due to the fact that the SVG is actually resides inside a different namespace to the containing HTML. When you pass a HTML fragment to JQuery (<g id="parent" /> in your case) it is created in the namespace of the HTML document and not the SVG.

    JQuery isn’t really suited to creating and manipulating non-HTML elements, although you could achieve a half-way house by using a combination of native JavaScript + JQuery:

    $("#btn").click(function() {
        var el = document.createElementNS('http://www.w3.org/2000/svg', 'g');
        var $el = $(el);
        $el.attr('id', 'parent');
        $("#container svg > *").wrapAll($el);
    });
    

    I’ve previously used JQuery to successfully manipulate SVG elements using Keith Wood’s excellent JQuery plugin. You may want to take a look at that.

    This answer discusses SVG and HTML namespaces in more detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to dynamically load a Javascript file using jQuery I keep getting a
I'm trying to dynamically create alert messages using the jQuery plugin for Bootstrap CSS.
I'm stuck. I'm trying to dynamically define a class method and I can't wrap
I am trying dynamically to append an <svg> element to an existing SVG island
So I'm trying to dynamically change the style of a div with javascript, which
I'm trying to dynamically create this function: svg.path($('#shape23'), path.move(333,410) .smoothC(333,410,333,410) .smoothC(217.5,415.75,217.5,415.75) .smoothC(333,500,333,500) .close(), {fill:
I am trying to dynamically insert a set of script tags with some javascript
I am trying to dynamically wrap the contents of a document's body tag in
I am trying to implement an interface where users can dynamically enter text and
I am trying to wrap a dynamically loaded image with a link tag: var

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.