views:

55

answers:

2

I am using a e-commerce package and there are sections my client wants disable to hidden. The problem is the package has all their labels under the same class and I am not sure how I can target certain ones in order to hide or disable them.

I have 5 and would like to hide the 3rd one, lets say. They are in a table to so it looks just like this:

<td><span class="lable">Description</span></td>

any suggestions?

+3  A: 

Since you've tagged this as jQuery, then the eq, gt and lt selectors may be of interest to you:

$("tr > td > span[class=lable]:eq(2)").hide();

Demo: http://jsfiddle.net/6MvnB/

and: http://jsfiddle.net/LD6nU/ (courtesy of @Majid)

karim79
+1 Very good selector, but I changed your code slightly to: `$("tr > td > span[class=lable]:eq(2)").hide();` It just hides the 3rd one not all greater than the 3rd as your selector does. But surely it is still your answer. Link: http://jsfiddle.net/LD6nU/
Majid
@Majid - Thank you. Somehow I got it into my head that he wanted everything after the whateverth to be hidden (I think I must have got that impression from sample markup which looks like a header row).
karim79
+2  A: 
$('.label').eq(2).hide()

Prefer the the list selection methods like eq() to the non-standard selectors like :eq. Using the jQuery selectors makes the browser fall back to the relatively slow Sizzle selector library instead of the fast native querySelectorAll() support built into modern browsers.

bobince
+1 Very elegant. And very interesting information about what goes on under the hood. Could you point where such inside knowledge is found?
Majid
line 3530 in jquery 1.4.2 source, plus the [CSS3 Selectors](http://www.w3.org/TR/css3-selectors/) spec. Unfortunately the jQuery docs don't spell out the implications for performance, but IMO using non-CSS selectors is questionable in itself. If a later CSS version comes along and defines one of the Sizzle/jQuery selectors with a slightly different meaning, we're all in trouble.
bobince
Awesome. Thank you very much. This worked great!
Xtian