Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for all elements starting with a prefix

I have several polymer elements.

<ps-el-one/>
<ps-el-two/>
<ps-el-three/>
<ps-el-four/>

I want to be able to query all of the elements which begin with "ps-" with either a CSS selector or javaScript.

I whipped up the following solution, but I am wondering if there is anything more efficient?

var allElementsOnPage = document.querySelectorAll('*');
var res = [];
for (var index in allElementsOnPage) {
  var el = allElementsOnPage[index];
  if (el && el.tagName && el.tagName.substr(0, 3) == 'PS-') {
    res.push(el);
  }
}

This solution seems very inefficient.

like image 708
Matt Gardner Avatar asked Nov 30 '16 20:11

Matt Gardner


People also ask

Is there a CSS selector by class prefix?

This is not possible with CSS selectors.

What is the prefix for a CSS ID selector?

The #id selector allows you to target an element by referencing the id HTML attribute. Similar to how class attributes are denoted in CSS with a “period” ( . ) before the class name, ID attributes are prefixed with an “octothorpe” ( # ), more commonly known as a “hash” or “pound sign”.

What CSS selector begins with a period?

Class Selectors Match an element that has the specified class. To match a specific class attribute, we always start the selector with a period, to signify that we are looking for a class value. The period is followed by the class attribute value we want to match.

How do I select a specific element in CSS?

The CSS [Attribute ~= “value”] can select the element if it includes the word “value” in the attribute specified. For example, the following code looks for the value “shirt” in the attribute “title”. The above code selects the first two paragraphs since they have a shirt in their title, which is the target attribute.

What is a CSS class selector?

The CSS class selector has a specific selector property to satisfy the use case requirement when the developer needs to select only a particular element rather than all with the same class. For example, I just want my “p” tag to be coloured red, but I have other elements too with the same class name and tag name defined.

How do you target the prefix in a selector?

All we did that’s different than the ends with ($=) selector is target the prefix by adding a ^ symbol in front of the = sign. You may also notice this time I used a class instead of an id. CSS is flexible enough to pretty much let you target anything as I stated earlier.

How to select an element with a repetitive suffix in CSS?

Selecting an element with CSS that has a repetitive suffix would look like this: The first thing to note is the div with the brackets surrounding the id$ attribute. The div tag can be any element in the DOM from input to span and so on. This is awesome if you’re trying to target a specific element that has a dynamic prefix like this:


1 Answers

Check this here

<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">

<script>
$( "input[name*='man']" ).val( "has man in it!" );
</script>
like image 126
Marios Nikolaou Avatar answered Sep 17 '22 21:09

Marios Nikolaou