Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts

    Apply Style to Textbox

    Hello,

    Using Javascript,

    How would I go about apply the following code below as a style class (Error) to a single textbox?

    Here's what I have so far:
    Code:
    document.getElementById('T1').style.backgroundColor = 'FEF6F4'
    document.getElementById('T1').style.border = '1px solid #CD0A0A'
    Thanks.

    J

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    A very basic one:
    Code:
    <style type="text/css">
    .error {
    	background: #fef6f4;
    	border:1px solid #cd0a0a;
    }
    </style>
    <script type="text/javascript">
    function showErr(el) {
    	el.className = el.className=='error'?'':'error';
    }
    </script>
    Try to clicking on the textbox for now<br>
    <input type="text" onclick="showErr(this);">
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    jason_kelly (10-05-2011)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <head>
    <style type = "text/css">
    .Error {background-color: #FEF6F4; border:1px solid #CD0A0A;}
    </style>
    </head>
    
    <body>
    
    <input type = "text"  id = "T1">
    
    <script type = "text/javascript">
    document.getElementById("T1").className = "Error"
    </script>
    </body>
    You might wish to make the background color more striking.


    This year, if you want to win the Premiership, you are going to have to finish above Manchester United. - Commentator, Talksport
    Last edited by Philip M; 10-04-2011 at 09:55 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    jason_kelly (10-05-2011)

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Looks good Philip, except from the stylesheet:
    Code:
    <style type = "text/css">
    .Error {backgroundColor = 'FEF6F4';border = '1px solid #CD0A0A'}
    </style>
    ...should have been:
    Code:
    <style type = "text/css">
    .Error {background-color = '#FEF6F4';border: 1px solid #CD0A0A;}
    </style>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    jason_kelly (10-05-2011)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by rangana View Post
    Looks good Philip, except from the stylesheet:
    Code:
    <style type = "text/css">
    .Error {backgroundColor = 'FEF6F4';border = '1px solid #CD0A0A'}
    </style>
    ...should have been:
    Code:
    <style type = "text/css">
    .Error {background-color = '#FEF6F4';border: 1px solid #CD0A0A;}
    </style>
    Yes, I know Raymond. I corrected it already!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    jason_kelly (10-05-2011)

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts
    Works great and a great big thanks to everyone on here for their help!

    You guys rock!

    Take care

    J


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •