-3

The

div[class^="test"]

selector will not match an element in markup if the element will be

class="  test-something"

(The intended selector had been generated on backend.)

The

div[class^="  test"]

is not an option.

Any suggestions?

Blix
  • 289
  • 2
  • 17
  • 1
    Won't `div[class*="test"]` work for your case? That is a contains match so have to be careful with usage. It would even match `class="detest"`. You could probably have a look at a combination of starts-with and contains selectors like mentioned [here](http://stackoverflow.com/questions/31140304/if-already-using-the-contains-selector-why-use-the-starts-with-selector/31140727#31140727) or [here](http://stackoverflow.com/questions/3338680/is-there-a-css-selector-by-class-prefix/8588532#8588532). – Harry Jul 08 '15 at 13:31
  • Please include your HTML so we know what we're targeting. Otherwise our answers will be just guesses. – TylerH Jul 08 '15 at 13:32
  • @TylerH You have markup on the second code example line. – Blix Jul 08 '15 at 13:36
  • 1
    Are you trying to match `test123`? If not, use `.test` or `[class~=test]`. – Oriol Jul 08 '15 at 13:37
  • @torazaburo what kind of question is this? – Blix Jul 08 '15 at 13:41
  • @Blix While there's not even enough markup in the post to reproduce the error, I have to assume that there's more to the class names than you've shared, since your title and post indicate that something should be following "test", but in your partial code, there's nothing. Otherwise, why use the `attr` selector at all? `.test {}` is what you should be using. – TylerH Jul 08 '15 at 13:42
  • @Blix *What kind of question is this?* I am saying, as have several other commenters, that if you want to match a `div` with the class `test`, all you need to do is to specify `div.test`. What exactly do you want to select? –  Jul 08 '15 at 13:46
  • @TylerH You have enough of info. The question was not about how to select element but about using "beginning with" selector in case there is a space between quotation and class name. – Blix Jul 08 '15 at 13:50
  • @torazaburo At first `div.test` is a bad practice. That should be `.test` and second I was asking about why the 'begin with' selector is not working on space. Not about how to select the element. Anyway, the answer had been answered. – Blix Jul 08 '15 at 13:53
  • 2
    @Blix but that's not what the `[attr^=value]` selector is for. You are using the "begins with" selector to select something that is at the end. You should know this won't work because *in your title* you refer to it as the "*begins* with" selector. Also, `div.test` is not bad practice at all. Who told you that? – TylerH Jul 08 '15 at 13:54
  • 3
    @Blix: How is that even relevant here? Fine, forget the others ever mentioned the tagname and just go with `.test` then. Does *that* work for you? If not, why not? – BoltClock Jul 08 '15 at 14:17
  • 3
    Also, I agree with @Harry that this is most likely a duplicate of http://stackoverflow.com/questions/3338680/is-there-a-css-selector-by-class-prefix judging by the situation you've described. – BoltClock Jul 08 '15 at 14:20
  • @Blix the reason `div.test` is bad practice in certain (and not all) cases is because it could be an overqualifying selector, which in that case `div[class^="test"]` would be just as well. – Stephan Muller Jul 08 '15 at 14:32

1 Answers1

1

What you did there ^= is:

'begins with..'

div[class^="test"] { }

which would work on something like this:

<div class="test and-some-class"></div>  
<!-- only if the very first string in your class matches "test" -->
<!-- so, class=" test something-else" won't work


You would have to use the CSS Selector:

'contains..'

div[class*="test"] { }

which would work on

<div class="class test something-else"></div> 
<!-- class name can be anything. "test" can be anywhere -->

Reference

Razvan B.
  • 6,721
  • 2
  • 19
  • 32
  • @torazaburo The first is an example of how to properly use the `[attr^=value]` selector. – TylerH Jul 08 '15 at 13:56
  • @torazaburo Firstly, I never said that the first one is an answer, is just an example of what he tried and how it is actually working. As for the my "second" that is the actual answer and yes, it will match `abctestdef` as well, but as far as I know, there is no other way of matching a class. Secondly, I guess it is an answer as it does what the OP was asking – Razvan B. Jul 08 '15 at 14:00