0

How can i use document.querySelector or document.querySelectorAll to get specific tag with any namespace.

I cannot use JQuery or any other library.

Ex

<mml:math>
    <mml:row>
    </mml:row>
  </mml:math>
<math style="display:none;">
  <mrow>
   <msup>
     <mfenced>
       <mrow>
         <mi>a</mi>
         <mo>+</mo>
         <mi>b</mi>
       </mrow>
     </mfenced>
     <mn>2</mn>
   </msup>
 </mrow>
</math>

 var tags = ['math:not([style*="display:none"])','mml\\:math:not([style*="display:none"])'];
                              document.querySelectorAll(mathmlVisibleTag.join()).length 

Here user can use any namespace. I have tried '*\\:math:not([style*="display:none"])' in querySelectorAll but it didn't worked.

Regards

2
  • You want to select any element which doesn't have display none? Commented Nov 1, 2017 at 12:44
  • @TKoL Yes. I want to find any visible math element. Commented Nov 1, 2017 at 13:07

2 Answers 2

0

I don't think this is possible with the only querySelectorAll. Maybe the best option is something like this:

function isVisible(e) {
    return !!( e.offsetWidth || e.offsetHeight || e.getClientRects().length );
}

var tags = document.querySelectorAll('*'); // change this to be more specific
var visibleMaths = [].slice.call(tags).filter(function (tag) {
    return tag.nodeName.search(/math/i) >= 0 &&
             isVisible(tag);
});

jsfiddle example

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't Array.from(tags) preferrable to [].slice.call(tags)? This resource says it's a lot faster, but apart from being faster it's also more obvious what it's doing.
I don't know if it is faster or not...it really depends on the browser. Anyway, I wrote it as an example, I didn't look at the details :)
-2

You just missed space, this one works fine:

document.querySelectorAll('math :not([style*="display:none"])');

1 Comment

@ePetkvo Above one is also working fine. I want to select math tag which could have any namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.