2

I have an external SVG loaded with the SVG Jquery plugin http://keith-wood.name/svg.html. One line of the SVG is:
<g groupmode="layer" id="layer1" inkscape:label="myLabel"></g>
I'm able to get the value of the label of that element by this line
$('#layer1', svg.root()).attr("inkscape:label");
this will return the value "myLabel".
How do I select the same element using that value?
Tryed with
var myElement = $('g[inkscape:label="myLabel"]', svg.root());
with no luck.

Any suggestion?
TIA!

ettolo
  • 267
  • 1
  • 3
  • 11

3 Answers3

2

according to this SO post referencing the jquery docs, your best bet would be var myElement = $('g[inkscape\\:label="myLabel"]', svg.root());.

Community
  • 1
  • 1
collapsar
  • 16,200
  • 4
  • 33
  • 60
  • have you tried iterating over the attributes of the `#layer1` element checking whether the `inkscape:label` attribute shows up at all and if it does under which name ? – collapsar Mar 21 '13 at 14:28
  • tryed iteration with: var attrs = $("#myElement")[0].attributes; for(var i=0;i " + attrs[i].nodeValue); } and the result is correctly: inkscape:groupmode => layer id => myElement inkscape:label => myLabel style => display:inline transform => translate(-292.15131,615.25519) but still can't select it by label value – ettolo Mar 26 '13 at 15:34
  • you are inspecting attributes on the dom level. have a look at the accepted answer of [this so post](http://stackoverflow.com/questions/2224933/iterating-over-element-attributes-with-jquery) to see how to iterate over all attributes as jquery knows them. this should reveal how to address the namespaced ones. – collapsar Mar 26 '13 at 15:52
  • @collapstar: thank you! but everything works if, inside the svg, i change "inkscape:label" to "label"; this way i can read label and select items by label – ettolo Mar 26 '13 at 16:05
0

It's been impossible for me to make it work as you request. The solution I have arrived, is duplicating the tag, but using an underscode instead of two dots, like this:

$('path').each(function() {
    $(this).attr({
        "inkscape_label" : $(this).attr('inkscape:label')
    });
});

Then I can select by the label I want like this:

$('[inkscape_label = ...
Sergio
  • 307
  • 1
  • 6
  • 18
0

For me the only solution that worked was this one:

const t = document.querySelectorAll('g') 
const layer = Array.from(t)
    .filter(t => t.getAttribute('inkscape:label') === 'myLabel')[0]
G M
  • 17,694
  • 10
  • 75
  • 78