1

I'm trying to add a class to multiple elements in a form. Here is my jQuery code:

$('input[type="text"],input[type="password"],select,input[type="submit"],input[type="checkbox"]').addClass("idleField");

It does work for all except for the checkbox. Any idea why?

Note: I have also tried doing it with these syntax:

:checkbox

#myForm :checkbox

Thank you!

4
  • More info needed. What browser is this happening on ? Does it happen all the time ? On load ? Can you group your checkboxes to a family and apply this class to the family and see ? Commented Mar 26, 2013 at 0:44
  • Let me put that in a jsfiddle for you. I am not seeing the problem. Commented Mar 26, 2013 at 0:47
  • This is happening on Chrome and Firefox, but works on IE. It is executed when the DOM is fully loaded. There is only one checkbox I need this to be applied on, and I tried giving the class in the CSS and it did not work. Commented Mar 26, 2013 at 0:51
  • Can you provide your CSS? Works for me in Chrome. Commented Mar 26, 2013 at 0:52

2 Answers 2

1

It works perfectly fine for me. Maybe you're styling your checkbox incorrectly?

jsFiddle

HTML

<input type="checkbox" /> 

CSS

.idleField {
    display:block;
    width:100px;
    height:100px;
}

JS

$(document).ready(function () {
    $('input[type="text"],input[type="password"],select,input[type="submit"],input[type="checkbox"]').addClass("idleField").addClass("idleField");
});

Update

It's working in your jsFiddle, your styles can't applying to the checkbox though. Checkboxes (and radio buttons) are determined by your operating system and can't be styled in the same manner as text boxes for example. Normally styling checkboxes or radio buttons involves hiding the original input and then creating a new div (or a label) and styling that maybe with a pseudo-element.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Here is a fiddle that could be useful: jsfiddle.net/F9wyh/1 As you see the checkbox has no styling applied, that's what it does on my code
1

LIVE DEMO

$(':text, :password, :submit, select, :checkbox').addClass("idleField");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.