Reputation: 157
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
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
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