Mario Lima Cavalcanti
Mario Lima Cavalcanti

Reputation: 157

Issue with jQuery addClass to input text box

I'm trying to apply a simple class to an input text box, but oddly it is not working.

$('body, :text').addClass('courier');
$('body, input[type=text]').addClass('courier');
$('body, #text').addClass('courier');
.courier {
      font-family: Courier New, serif;
      background-color: #000;
      color: #ccc;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<input type="text" id="text" />

I tried these three methods, but the changes are applied only to the body. I just also tried to add the class just to the input text.

$('body, :text').addClass('courier');
$('body, input[type=text]').addClass('courier');
$('body, #text').addClass('courier');

How can I do this?

Upvotes: 0

Views: 21469

Answers (2)

amdorra
amdorra

Reputation: 1556

you should write

$('body input[type=text]').addClass('courier');

or

$('input[type=text]').addClass('courier');

you can see it in this fiddle

Upvotes: 3

Charlie74
Charlie74

Reputation: 2903

Since your element already has an ID... use that for reference...

This should be the simplest way to add a class to your input:

$('#text').addClass('courier');

Upvotes: 1

Related Questions