Monday, June 01, 2009

It's been a little while

So with all of the fancy iPhone, AppEngine, GWT, Android development I've been involved with I was on the job the other day and someone asked me how to do something in Javascript.  Her problem was a tricky one but it basically built upon a very simple foundation.  When a user selected something she needed some text and a style to change.  I knew immediately how to do it but the syntax escaped me for a bit so I thought I'd stick it out here for the next time I needed it.

The problem is pretty simple click a button change some text and the color of the text.  So here is my very simple solution.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
   <title> Switch </title>
      <script type="text/javascript">
         function change()
         {
            var styleClass = 'required';
            var textValue = 'Else'
            if (document.getElementById('sigDate').className == 'required')
            {     
               styleClass='standard';
               textValue='Something';
            }
            document.getElementById('sigDate').innerHTML=textValue;
            document.getElementById('sigDate').className=styleClass;
         }
      </script>
      <style type="text/css">
         .required{
            color: RED;
         }
         .standard{
            color: BLUE;
         }
      </style>
   </head>
   <body>
      <input type="button" value="Switch" onclick="change()" />
      <div id="sigDate" class="standard">
         Something
      </div>
   </body>
</html>

Like I said a very simple solution to build a very complex solution upon.

 

-Aaron

No comments: