248

I'm currently working on a project where I have no control over the HTML that I am applying CSS styles to. And the HTML is not very well labelled, in the sense that there are not enough id and class declarations to differentiate between elements.

So, I'm looking for ways I can apply styles to objects that don't have an id or class attribute.

Sometimes, form items have a "name" or "value" attribute:

<input type="submit" value="Go" name="goButton">

Is there a way I can apply a style based on name="goButton"? What about "value"?

It's the kind of thing that's hard to find out because a Google search will find all sorts of instances in which broad terms like "name" and "value" will appear in web pages.

I'm kind of suspecting the answer is no... but perhaps someone has a clever hack?

Any advice would be greatly appreciated.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Questioner
  • 7,133
  • 16
  • 61
  • 94

10 Answers10

399

You can use the attribute selector,

input[name="goButton"] {
  background: red;
}
<input name="goButton">

Be aware that it isn't supported in IE6.

Update: In 2016 you can pretty much use them as you want, since IE6 is dead. http://quirksmode.org/css/selectors/

http://reference.sitepoint.com/css/attributeselector

dota2pro
  • 7,220
  • 7
  • 44
  • 79
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
57

Text Input Example

input[type=text] {
  width: 150px;
}

input[name=myname] {
  width: 100px;
}
<input type="text">
<br>
<input type="text" name="myname">
dippas
  • 58,591
  • 15
  • 114
  • 126
MRRaja
  • 1,073
  • 12
  • 25
22

You can use attribute selectors but they won't work in IE6 like meder said, there are javascript workarounds to that tho. Check Selectivizr

More detailed into on attribute selectors: http://www.css3.info/preview/attribute-selectors/

/* turns all input fields that have a name that starts with "go" red */
input[name^="go"] { color: red }
ChrisR
  • 14,370
  • 16
  • 70
  • 107
14

For future googlers, FYI, the method in the answer by @meder , can be used with any element that has a name attribute, so lets say theres an <iframe> with the name xyz then you can use the rule as belows.

iframe[name=xyz] {    
    display: none;
}   

The name attribute can be used on the following elements:

  • <button>
  • <fieldset>
  • <form>
  • <iframe>
  • <input>
  • <keygen>
  • <map>
  • <meta>
  • <object>
  • <output>
  • <param>
  • <select>
  • <textarea>
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
  • 3
    this doesn't really add anything to the question. The `name` attribute can be put on anything if you really wanted to – Isaac Oct 05 '16 at 23:25
  • Moreover, some people don't use Google search engine. Therefore, the word "googlers" is not very inclusive. – gouessej Mar 29 '23 at 14:13
6

Using [name=elementName]{} without tag before will work too. It will affect all elements with this name.

For example:

[name=test] {
  width: 100px;
}
<input type=text name=test>
<div name=test></div>
dota2pro
  • 7,220
  • 7
  • 44
  • 79
Batsheva Hansav
  • 316
  • 2
  • 11
4

If i understand your question right then,

Yes you can set style of individual element if its id or name is available,

e.g.

if id available then u can get control over the element like,

<input type="submit" value="Go" name="goButton">

var v_obj = document.getElementsById('goButton');

v_obj.setAttribute('style','color:red;background:none');

else if name is available then u can get control over the element like,

<input type="submit" value="Go" name="goButton">

var v_obj = document.getElementsByName('goButton');

v_obj.setAttribute('style','color:red;background:none');
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
Jitesh
  • 1,384
  • 10
  • 20
2

if in case you are not using name in input but other element, then you can target other element with there attribute.

    [title~=flower] {
      border: 5px solid yellow;
    }
    <img src="klematis.jpg" title="klematis flower" width="150" height="113">
    <img src="img_flwr.gif" title="flower" width="224" height="162">
    <img src="img_flwr.gif" title="flowers" width="224" height="162">

hope its help. Thank you

Ali Raza
  • 673
  • 8
  • 19
2

input[type=text] {
  width: 150px;
  length: 150px;
}

input[name=myname] {
  width: 100px;
length: 150px;
}
<input type="text">
<br>
<input type="text" name="myname">
1

This is the perfect job for the query selector...

var Set1=document.querySelectorAll('input[type=button]');  // by type

var Set2=document.querySelectorAll('input[name=goButton]'); // by name

var Set3=document.querySelectorAll('input[value=Go]'); // by value

You can then loop through these collections to operate on elements found.

-2

have you explored the possibility of using jQuery? It has a very reach selector model (similar in syntax to CSS) and even if your elements don't have IDs, you should be able to select them using parent --> child --> grandchild relationship. Once you have them selected, there's a very simple method call (I forget the exact name) that allows you to apply CSS style to the element(s).

It should be simple to use and as a bonus, you'll most likely be very cross-platform compatible.

DXM
  • 4,413
  • 1
  • 19
  • 29