2

So i want to hide all elements and use

$('[id^=option_]').hide();

And that works fine, however it is hiding things i dont want it to hide. I have dynamically generated ids such as

option_1 option_1_form option_2 option_2_form

So as you can see it will also hide the forms. So i was wondering if there was a way to either filter _form out or use a regular expression of some sort for the selector

('[id^=option_][id$=/[^0-9 ]+/]')

Not that the example would work, but an example of what i mean! This would force the _form to be excluded since it would have to end in a numeric value. If you know of a better way, i am all for it too. I am no jQuery wiz.

Austin Best
  • 1,028
  • 4
  • 14
  • 30
  • Your proposed solution looks for something ending with a number but your description seems to imply that you want to just not include `_form`. If you do in fact only want to exclude things with `_form` that is easier and does not require regex. – James Montagne Oct 14 '13 at 20:50
  • I am looking for either or, as i am not sure which is easier/better/more efficient with jQuery. Kinda why i left it open for interpretation :) – Austin Best Oct 14 '13 at 20:57

2 Answers2

5

You can try using :not to exclude forms

$('[id^=option_]:not(form)').hide();
Adil
  • 146,340
  • 25
  • 209
  • 204
  • This did the trick, thanks. However do you know if "not" would be better usage than "filter"? Seems filter would have to run through the dom a second time to remove anything it doesn't want where as not can do it while in the dom the first time? Either way i will accept this because it does work. – Austin Best Oct 14 '13 at 20:53
3

If you just want to exclude anything containing _form in the id:

$('[id^=option_]:not([id|=_form])').hide();

|= is for "contains".

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • And the difference in yours and the one above? As his works without the id|= as well. Still learning these little tricks. – Austin Best Oct 14 '13 at 20:54
  • @AustinBest The other answer excludes `
    ` elements which is very different than excluding an element with an `id` that contains the string `_form`.
    – James Montagne Oct 14 '13 at 20:55
  • So in short, yours is a more specific way of the one above? – Austin Best Oct 14 '13 at 20:56
  • It's two different things. If you want to exclude `
    ` elements then the other answer is better. If you can have elements which are not `
    ` that can have `_form` in their `id` that you also want to exclude, this will work. For instance, `` would be excluded with my code and included with the other.
    – James Montagne Oct 14 '13 at 20:57
  • Ha, i get what you are saying now. TY – Austin Best Oct 14 '13 at 21:00