jQuery Form How to - Add CSS Class to input text box








Question

We would like to know how to add CSS Class to input text box.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-git.js'></script>
<style type='text/css'>
.courier {<!--   w  ww  .j  av  a2s  .co  m-->
  background-color: red;
  color: #ccc;
}
</style>
<script type='text/javascript'>
$(function(){
    $('input[type=text]').addClass('courier');
});
</script>
</head>
<body>
  <form>
    <input type="text" id="text" />
    <input type="button" id="button" value='OK' />
  </form>
</body>
</html>

The code above is rendered as follows: