1

I have a default class for my form which is defining some styles rules (scss format) like this:

form.simple_form {

  $indent: 200px;   
  $length: 350px;
  .... 
}

In some cases, I need other set of style rules to be applied to my forms, but I am not able to remove the simple_form default class from them (each form in my application is created with simple_form class).

Fortunately, I am able to set additional class of each form and using not css selector I am able not to apply the default form class for each form like this:

form.simple_form:not(.signup, .login) {

  $indent: 200px;
  $length: 350px;
  ....
}

That is working perfectly, but for some reason, the simple_form class is not applied for all forms no matter that they have not got the signup or login classes.

So, why the css selector is not able to get a form with simple_form class? Something more, If I paste in the console the following code:

$('form.simple_form:not(.signup, .login)')

It successfully returns the form.

Please note, that I am using ruby on rails and simple_form gem.

EDIT:

This is the HTML of the form that I want to select:

<form accept-charset="UTF-8" action="/webinars" class="simple_form new_webinar" data-remote="true" data-type="js" data-validate="true" enctype="multipart/form-data" id="new_webinar" method="post" novalidate="novalidate">
....
</form>

This is the HTML that I do not want to select:

<form accept-charset="UTF-8" action="/sessions" class="simple_form login" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="kmDnV10Pv3eQz09U9QKTjfLQ7zweu6teALh4DDjYmfk="></div>
...
</form>
Nick
  • 11,475
  • 1
  • 36
  • 47
gotqn
  • 42,737
  • 46
  • 157
  • 243
  • Please show an example of the HTML you wish the CSS rule to match and the HTML you wish the rule to not match. – jfriend00 Jun 08 '13 at 08:32
  • @jfriend00 I have edited the question and post both the forms I do want and do not want to select. – gotqn Jun 08 '13 at 08:45
  • I think that $('form.simple_form:not(.signup, .simple_form)') will return all the forms, even signup. I think that the correct syntax is $('form.simple_form:not(.signup):not(.simple_form)') – vals Jun 08 '13 at 09:01
  • What browser are you testing in? – Frederick Cheung Jun 08 '13 at 09:02
  • Chrome (Version 27.0.1453.110) using Ubuntu 13.04. I should have CSS3 selectors support. – gotqn Jun 08 '13 at 09:04
  • @vals Sorry about this, I have make a mistake - it is sigup and login classes that I do not want to select. I have edited the example. – gotqn Jun 08 '13 at 09:06
  • a possible workaround is to use the attribute selector and override the applied styles with more specificity. form[class="simple_form login][reset rules property: value !important;} – Jawad Jun 08 '13 at 09:22

1 Answers1

3

The problem you're having seems to be the use of a comma-separated list of selectors within the :not() selector, each element to be removed must be selected ('unselected'?) one at a time (certainly in Chromium 25, Ubuntu 12.10).

Given the following HTML:

<p>No classes</p>
<p class="test">With class 'test'</p>
<p class="test1">With class 'test1'</p>

The following works:

p {
    color: #f00;
}

p:not(.test):not(.test1) {
    color: #000;
}

JS Fiddle demo.

Whereas the following (similar approach to yours) does not:

p {
    color: #f00;
}

p:not(.test, .test1) {
    color: #000;
}

JS Fiddle demo.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • 1
    An explanation can be found in the duplicate I've just linked to - it's a discrepancy between jQuery and CSS. – BoltClock Jun 08 '13 at 09:22
  • Check this - http://jsfiddle.net/YgAu9/2/ - this is my case and it is not working. – gotqn Jun 08 '13 at 09:28
  • 1
    @BoltClock My question does not have anything in common with the ref one by you. I need a working CSS selector. I am not asking why the jQuery gives me the result. – gotqn Jun 08 '13 at 09:29
  • 1
    @BoltClock: what does this have to do with jQuery? – Jawad Jun 08 '13 at 09:31
  • @David Thomas, Thanks, this helps me to solve my issue http://jsfiddle.net/YgAu9/3/ – gotqn Jun 08 '13 at 09:40
  • 1
    @Jawad: The console code that the OP is using works, so it must be jQuery. No other library supports `:not()` like that. – BoltClock Jun 08 '13 at 10:29
  • 1
    @gotqn: How are the questions different? Both are asking why the selector doesn't work in CSS, but works in jQuery, and how it can be done using CSS, and the answers both provide the same solution. – BoltClock Jun 08 '13 at 10:30
  • @BoltClock Yes, truly in the link you can find the solution offered by David Thomas. – gotqn Jun 08 '13 at 11:59