views:

46

answers:

2

Hello, let's say that I have some inputs with ID like that:

ctl139_ctl00_txtValue
ctl140_ctl00_txtValue
ctl141_ctl00_txtValue
.....
xxxxxx_ctl00_txtValue

how, using CSS, apply the same style for these inputs (where ID follows the pattern: xxxx_ctl00_txtValue)

I tried CSS3 as Minkiele suggessted but I've done the following:

[id*="_ctl00_txtValue"]{ /* RULES */ } 

then it should apply style for all elements where id contains my string. It works in Chrome but not in IE8 but msdn tells that it should work. Why it doesn't work?

+9  A: 

You could use css3 selector ends in: $=, but I don't know how many browsers implement this feature.

input[id$="_ctl00_txtValue"]{
   /* RULES */
}

If you can change input markup, I suggest to add a class to this elements, which for styling purpose is simpler.

CSS3 Selectors

Update

Quirksmode has a test page for this selectors type. It seems the only browsers not supporting this feature are (surprise surprise) IE 5.5 and 6

Minkiele
+1 for CSS3 Selectors. Its awesome and I had no idea!
Gaurav Saxena
I tried:[id*="_ctl00_txtValue"]{ /* RULES */} it works in Chrome but not in IE8 but I checked in msdn and it should work
niao
what is the definition I need to add to change the DOC type in order to not use Quirksmode
niao
Sorry dude but *Quirksmode* is a website with a page (linked in the answer) that tests that CSS feature.
Minkiele
Very odd that it doesn’t work in IE 8. Here are test cases for the “contains” and “ends with” selectors, do they work in your copy of IE 8? http://www.w3.org/Style/CSS/Test/CSS3/Selectors/current/html/tests/css3-modsel-10.html, http://www.w3.org/Style/CSS/Test/CSS3/Selectors/current/html/tests/css3-modsel-10.html
Paul D. Waite
@Minkiele Sorry dude, “quirks mode” is also a term for a rendering mode that some browsers have.
Paul D. Waite
@Minkiele: try putting this doctype as the first thing in your page: `<!doctype html>`
Paul D. Waite
no it doesn't work in quirksmode
niao
@Paul D. Waite I know, In my answer and in the comment I should have clarified that I was referring to the website, not the rendering mode.
Minkiele
@Paul D. Waite - when I set additionally <!doctype html> it now works fine
niao
@Minkiele — ah, sorry, gotcha.
Paul D. Waite
@niao: for more on the nightmare that is quirks modes and doctypes, see http://hsivonen.iki.fi/doctype/
Paul D. Waite
+1  A: 

The fact you have no control over the generated HTML makes it difficult.

If you've not adversed to jQuery and some slight DOM overhead, this will work:

Get a hold of the container for all the items, let's say their in a div. Keep this selector as tight as possible, as the "looser" it is, the more elements to process and thus more overhead.

var pattern = '_ctl00_txtValue';
$('#somecontainer input').each(function() {
   if ($(this).attr("id").indexOf(pattern) > 0) {
      // Apply CSS using jQuery, e.g:
      $(this).css("color","red");
   }
});

Basically you're looping through the DOM elements within a given container, checking if the ID contains the pattern you want, then if it does, apply your CSS.

That should work, but as i said, there is some DOM traversal overhead depending how your HTML is structured.

RPM1984