0

HTML:

<a class="someclass-random1 selected">The third div element.</a>
<p class="selected someclass-random2">This is some text in a paragraph.</p>

Selector: $('*[class^=someclass]')

This selector works on <a>, but not on <p>. Why would that be?

jsfiddle link

Edit: corrected typo

Amit G
  • 2,293
  • 3
  • 24
  • 44

2 Answers2

1

The expression ^= equates to "starts with" so

$('*[class^=someclass]')

is looking for any element with a classname that starts with someclass

Hence a matches and p does not

Jonathan Crowe
  • 5,793
  • 1
  • 18
  • 28
1

Try this way:

$.expr[':'].startingClass = function(obj){
  return ((/\bsomeclass/).test(obj.className));
};

$('*:startingClass').click(function(){
        $(this).hide();
});

demo

For more detail: see here


Or, simply you can use like this:

$('[class*=someclass]')

The * would search anywhere instead of starting from first.

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231