0

Better explain my problem using examples:

I have a html file that contains something like

<a name="2846" href="Data.Product.html#2846" class="Function"> ... </a>
<a name="2877" href="Data.Product.html#2846" class="Function"> ... </a>
<a name="2911" href="Data.Product.html#2911" class="Function"> ... </a>

I want to select (by means of CSS selectors) elements that have class "Function", with href attribute ends with the exact value of name attribute. In the example above, only the following two will be selected

<a name="2846" href="Data.Product.html#2846" class="Function"> ... </a>
<a name="2911" href="Data.Product.html#2911" class="Function"> ... </a>

I think about that, maybe something along the line a.Function[href$=?]. My question is can I refer to the value of name attribute in a selector, is there any way to do that?

Thanks!

Jeremy Bi
  • 31
  • 2

1 Answers1

0

Two loops are necessary in your case. One to fetch all Function class elements and then check whether the href attribute has the name attribute value in it.

Try this:

var filteredEls = Array.prototype.filter.call( // Convert NodeList to Array
  document.getElementsByClassName('Function'), // Get all "Function" class elements
  function(el) {
    return el.getAttribute('href') === "Data.Product.html#" + el.name; // Filter elements with the condition
  }
);

console.dir(filteredEls); // check console
<a name="2846" href="Data.Product.html#2846" class="Function"> ... </a>
<a name="2877" href="Data.Product.html#2846" class="Function"> ... </a>
<a name="2911" href="Data.Product.html#2911" class="Function"> ... </a>
Mr_Green
  • 40,727
  • 45
  • 159
  • 271