Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Selectors in Javascript with all H tags

Using Prototype JS library, I want to select all child link elements (A tags) regardless of whether their parent is: H1, H2, H3, H4, or H5 (etc) with a simple CSS Selector Rule (as opposed to further JS, like looping etc).

So the simple, but long way, to do this is:

$('page').select('h1 > a, h2 > a, h3 > a, h4 > a, h5 > a')

I guess I'm looking for a wild-card property, like h*, that doesn't exist.

Maybe the above example is the way to go, but I'm hoping there is a simpler, more concise and efficient way to do this.

Tips appreciated.

like image 599
donohoe Avatar asked Oct 22 '09 19:10

donohoe


People also ask

How do you target all h1 in CSS?

To select all level 1 headings we would use the h1 selector. Element selectors will apply CSS to every instance of that element in your document.

Can you use CSS selectors in JavaScript?

In CSS, selectors are patterns used to select the element(s) you want to style, but as you can tell from the title above, selectors are also useful in javascript and below are some examples on how to use them.

What CSS selector matches all list items?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

Which selector is used to assign the style to all h1 elements?

The above selector is based on the type of the element: it selects all elements of type "h1." This kind of selector is called type selector .


2 Answers

As per Fabien Ménager comment to the original question, it looks like there isn't a simple CSS Selector I can use other than what I have already.

$('page').select('h1 > a, h2 > a, h3 > a, h4 > a, h5 > a')

While there are other options if I want to be programmatic about it, or empty JQuery (which isn't an option for me) I am specifically looking for a CSS rule.

Thank you to everyone who tried to help.

like image 94
donohoe Avatar answered Sep 18 '22 12:09

donohoe


Using jQuery:

$(":header a")
like image 28
Josh Stodola Avatar answered Sep 18 '22 12:09

Josh Stodola