1

I want to get all elements from document which starts with ge-. Example <ge-survey></ge-survey> <ge-sombody></ge-sombody> <ge-...></ge-...>.

var elements = document.getElementsByTagName("*"); // <<-- now return all

also I tried :

var elements = document.querySelectorAll("^=ge-");

but get :

geadele.js:3 Uncaught SyntaxError: Failed to execute 'querySelectorAll' on 'Document': '^=ge-' is not a valid selector.

Also I tried:

var els = document.getElementsByTagName("^=ge-"); // return epty HTMLcollection

my html:

...
</head>
<body>
  <ge-survey></ge-survey>
  <ge-element></ge-element>
</body>
...
3

2 Answers 2

1

If you know all ge-elements, which you want to get, you can use document.getElementsByTagName('ge-somebody') and concatenate result.

var ge1Elements = Array.prototype.slice.call(document.getElementsByTagName('ge-1'));
var ge2Elements = Array.prototype.slice.call(document.getElementsByTagName('ge-2'));
...
var geElements = [].concat(ge1Elements, ge2Elements, ge3Elements);

In my opinion this approach in documents with a large number of elements is faster than regexp, but I didn't any benchmarks.

If you don't know all posible ge- elements, you can get it by filtering all elements:

var elements = document.getElementsByTagName("*");
var geElements = [];
for(var i = 0; i < elements.length; i++){
    if(elements[i].tagName.indexOf('GE-') === 0){
      geElements.push(elements[i]);
    }
}
console.log(geElements);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried it but I thing is better use prepared JavaScript decision with reg expressions, not need write more code;
I dont know about ge elements in html.
@EdgarasKarka If you don't know a set of all ge-tags, you should use the second approach with filtering any collection of html-elements by tagName matching
0

You can use document.querySelector and use a 'starts with' type css selector: Can I use a regular expression in querySelectorAll?

1 Comment

I tried document.querySelectorAll("^=ge-"); but get error. Look to post

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.