1

I am working on an ASP.NET page in which I generate some JavaScript code based on values in a database.

For example, in the database there might be the formula sum([#itemA]), which would evaluate to something like $('#itemA').change(function(){ sum(this); });.

I know that I can select elements using jQuery selectors such as $('[id*=itemA]') or $('[class*=itemA]'), but I am wondering if there is any way to combine these selectors so that any element with either a class or an ID of itemA would be selected.

Right now in my code I am just using if/else blocks to deal with ID's or classes, but if there is an easier implementation, I would love to use it.

I looked at the jQuery documentation and googled around a bit, but I didn't see anything that answered my question.

Thanks in advance!

Paul Woidke
  • 928
  • 4
  • 18
  • 40

3 Answers3

3

This should do the trick

$('[id*=itemA], [class*=itemA]')

For more details, take a look to official jquery documentation

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • This is the correct answer. OP is looking for either class OR id. Not class within an id. – Johan Mar 25 '13 at 15:59
  • This is perfect. I've been so focused on the Javascript so much that I totally forgot to think in terms of CSS selectors. – Paul Woidke Mar 25 '13 at 16:01
1

Use multiple selectors like

    $('.class1, .class2....., .classn')

You can literally use anything like ID names, class names or selectors like you specified separated by commas

theshadowmonkey
  • 689
  • 8
  • 26
0

Like in css, when selectors are separated by ,they are cumulated :

var elements = $('[id*=itemA], [class*=itemA]')
Johnny5
  • 6,664
  • 3
  • 45
  • 78