Owen L.
Owen L.

Reputation: 346

CSS Selector for name attribute of <a>?

This may be a dumb question, but what is the CSS Selector for the attribute of <a> that is "name"?

document.body.innerHTML = myString.anchor("HTML_String")

This JavaScript creates a <a> element with the name "HTML_String." How do I access only this element in my CSS?

Upvotes: 7

Views: 10406

Answers (4)

Softwaretechniker
Softwaretechniker

Reputation: 29

[name="yourName"]

or if it only should have any name:

[name]

an example for an input:

input.myClass[name="greatName"] {
    ...
}

But if your question is how to look for something within your "innerHTML": This isn't possible.

Upvotes: 3

NYG
NYG

Reputation: 1817

There is the [name="name"] selector but it's not really cross-browser. Old versions of Internet Explorer don't support the selector by HTML attribute (that browser tho.........).

My suggestion is to always use classes for CSS (even for unique elements) and ids for JavaScript, while you'll leave the names for backend programming.

Add a class to the element and then a.myclass

Upvotes: 4

Quentin
Quentin

Reputation: 944431

CSS doesn't have a specific selector syntax for name attributes. You have to use the generic attribute selector syntax.

[att=val] Represents an element with the att attribute whose value is exactly "val".

Upvotes: 7

jeremy-denis
jeremy-denis

Reputation: 6878

use the [attribute=value] css selector, you can access it by using :

a[name=HTML_STRING] {
  //your css
}

Upvotes: 3

Related Questions