Simple Javascript to Mimic HTML5 Textbox Placeholder in ASP.NET Form

I’m doing a project at work that is unfortunately targeted mostly to IE8 browsers, so using HTML5 functionality for it is “iffy” at best.

However, there is one screen where I really wanted to mimic the HTML5 placeholder behavior (which is demonstrated in the iframe below, since stupid WordPress won’t let me write the tag in the HTML editor with the “placeholder” attribute…grrr!)

So anyway, to mimic this behavior, I needed to remove the default placeholder text when anyone clicked into the textbox.  I also wanted to have my placeholder text grayed out, but then make it darker when the user starts typing.  I also have the added joy of writing this all in ASP.NET, which requires a little extra work around.  To the code!

Step one is to add the textbox to the page with default text already entered.  I also have a CSS class that makes the text light gray.

<asp:TextBox ID="tbExample" runat="server" Text="Placeholder Text Goes Here" CssClass="lite-gray"></asp:TextBox>

Next, since it’s .NET, I had to add the ‘onclick’ and ‘onblur’ events in the codebehind.  I just stuck them in the Page_Load event.

protected void Page_Load(object sender, EventArgs e)
{
     if (!Page.IsPostBack)
     {
          tbExample.Attributes.Add("onclick", "mimicPlaceholder()");
          tbExample.Attributes.Add("onblur", "mimicPlaceholder()");
     }
}

So now it’s just left to write the JavaScript.

function mimicPlaceholder()
{
     var myTextBox = document.getElementById('<%= tbExample.ClientID %>');

     if (myTextBox.value == "Placeholder Text Goes Here")
     {
          myTextBox.value = "";
          setToBlack(myTextBox);
     }
     else if (myTextBox.value == "")
     {
          myTextBox.value = "Placeholder Text Goes Here";
          setToGray(myTextBox);
     }
}

And in my jQuery scripts file, I add these two functions

function setToBlack(field) {
     $(field).removeClass("lite-gray");
}
function setToGray(field) {
     $(field).addClass("lite-gray");
}

So, to explain the code…the tbExample.Attributes.Add portion adds the onclick and onblur events to the text box. That will make sure that the javascript function mimicPlaceholder() will get called whenever anyone clicks into the tbExample text box or clicks out of the tbExample text box.

The mimicPlaceholder javascript function assigns the textbox itself to a variable “myTextBox”. Then, it checks to see if the default value (“Placeholder Text Goes Here”, in this case) is present in that text box. If so, it removes the default text and calls the setToBlack function to remove the “lite-gray” class from the textbox. If the field is empty, however, it will add that value back INTO the textbox and ADD the “lite-gray” class to the textbox.

In theory, it’s entirely possible for the field to somehow get things messed up and click into an empty textbox which would then populate it with the default text, making the user have to clear it out manually. However, (again, in theory) a user will always be clicking into the field for the first time with the default text there, and it should be impossible for them to leave the field empty without the javascript filling it back in with the default text. (And if they leave the field with another value in place, I’d prefer it remained there instead of the default text, which is why I only fill it back in if the field is blank.)

It’s rather crude, but it’s working like a charm so far.

4 Quick Wins Thanks To LESS

As promised in my last post, here are 4 quick wins that LESS can easily get you, even if you are a n00b such as myself…

The LESS code becomes this CSS code
  1. Variables – It’s pretty easy to see how sweet variables in CSS can be.  Declare a variable to hold your chosen font stack, or to set your color palette with easily reproduced names, rather than having to remember hex values.  Also, if you do all this in one spot at the top of your stylesheet, you can easily locate these values and change them all at once, instead of CTRL-F’ing your way through your code.  I’m actually fairly surprised it took an entirely new language to get this working, but LESS has finally given us the variables we’ve always wanted for CSS!
  2. Colors – While it’s true that colors are greatly helped by the ability to use variables, there is even more you can do with your color palette in LESS.  My favorite thing is to use the darken() and lighten() functions to help me create my color palettes.  I’ve always been a little helpless when it comes to picking degrees of color. Thank goodness it’s not necessary anymore!  (By the way, lighten() and darken() are just the first color functions I’ve discovered…there are even more such as saturate() and fadein(), but these two were certainly the easiest to wrap MY head around.)
  3. Nested Rules – I wasn’t sure about this one when I first read about it in the LESS documentation, but let me assure you that this is great!  It was almost love at first site when I began to use it.  Place rules for specific classes or ID’s right within the curly braces of that element, and you can quickly see how easy it is to use.  If you refer to the graphic I embedded on this article, you can see the new anchor color rules I added for the sidebar.  Dividing things like this is almost second nature right from the start!  (At least, for me it was!)
  4. Importing for organization - While I didn’t include this on the example illustration, the BONES template I use does an excellent job of showing how useful this can be.  They’ve broken their screen size styles into separate files…any browser above 768px wide (for tablets and such) will get styles in the 768up.less file, while anything above 1030px wide will get styles in the 1030up.less file…then they just include each of those files in a style.less file, broken out with CSS media queries (which is how responsive design works.  If none of this makes sense to you, don’t worry, it didn’t for me either before I discovered This is Responsive Design, which is just an amazing collection of everything you need to know about it.  I would especially start with this A List Apart article, which is where I believe it all began)

While the Bones template uses a lot more than the 4 LESS functionalities outlined above, these were the ones I immediately latched onto and started using right away.  Even if this were all LESS gave us, it would be worth it.  However, there are TONS of other things we can do with the language that I’m only starting to wrap my head around.  I’m sure there will be more to report on in the future, but for now, I’m just enjoying the bare minimum that I understand.  It’s enough to get me interested in the rest.

Liam Lynch on Quitting Smoking:

Liam Lynch (at least, a rough approximation)Liam Lynch, who I could listen to all day, was being interviewed on the Nerdist and had this to say about quitting smoking:

“If I wrote my day in a diary, cigarettes were my punctuation..every comma and period. I got in my car, period. I walked outside, comma…so removing that out of my life was like removing all of the punctuation out of my day.”

It was one of those quotes that expressed something I had always felt but until he said it I would never have been able to express. It kind of blew me away.

The First Thing WordPress Has Taught Me: LESS

In my first post, I outlined my “WordPress First” strategy, which is really nothing more than “let someone else do all the hard work for me so I can then dig into their code and see how they did it” strategy.  Which I still say is brilliant.

Anyway, the majority of the code I’m interested in comes from the responsive, HTML5 Boilerplate based theme Bones.  Having dug through some of the code, I figured the obvious first place to start was updating the stylesheets to give me a little more customized look before moving on to other things, and that’s what introduced me to LESS.

I recently got an email from an online education site advertising a new course covering LESS.  Which immediately caught my attention because the upshot I got from it was “Wait…variables in my CSS?!  How do I sign up for this?!!!”  So imagine my joy upon discovering Bones ships with LESS already “installed”.   It even had links in the documentation on how to set it up on my machine (because there is a little setup to get things working at first.)  Seeing as how there are a couple of different ways to do it, I’ll let you decide for yourself which is best for your environment by simply pointing you here for a quick introduction on LESS and it’s similar cousin Sass, which is where I was directed.  To be fair, most of the original articles I read comparing the two leaned more towards Sass, because it apparently is a bit more developed as a language and includes some conditional statements and loops that aren’t available in LESS.  It also doesn’t have some of the support for inheriting styles.  However, SASS requires Ruby to be installed on your machine…and I just stubbornly decided I didn’t want to do that.  Especially when LESS has a simple program like WinLess to sit and watch your LESS files and compile them into CSS each time they are saved.  So I went with LESS because I’m basically lazy and wanted to get to playing with the code.  (really hope I don’t regret that decision down the road)

Now what WinLess does is simple.  Once you install it, you point any .less files (or entire folders) that you would like monitored.  Then, either you can tell WinLess where you would like the resulting CSS files to be saved, or you can simply have your LESS and CSS folders on the same level, and WinLess will save your CSS files into the CSS folder for you automatically.  And then…you write you LESS code.  Each time you save a LESS file, the equivalent css file will be written where you have told it to be saved.  And…that’s it.

 

 

It will even warn you of syntax errors in your .less files.  As far as ease of use goes…it’s really hard to complain.

Next post, I’ll discuss the most useful components of LESS that I’ve used so far.

A “WordPress First” Strategy

For quite some time now, I have needed to do something with popmatic.com.  I’ve had this domain name since mid-2002 (w00t! 10 years!), and I’ve had everything from a fully functioning web site to an empty 404 since then.  To be honest, it’s been a long time since I had need of a website, other than just a spot online to try some goofy code or to post an occasional large file.  But the time has come.  While Facebook and Twitter can fill the void for posting adorable pictures of my kid or dumb videos of guys covering Celine Dion on a recorder, they just can’t give me the longform freedom of a blog.  So here we go.

So now that we have the “why?” out of the way, I guess the next question is “how?”.  I know a few things about this new and improved popmatic.com:

  1. I want it to have a blog
  2. I want it to have other stuff

So, as you can see, I have very high expectations.  There are a few other rules that I want to live by, however, outside of the content requirements of the site:

  1. The site should be responsive
  2. The site should be written using HTML5
  3. There should be space on the site for me to practice both PHP and ASP.NET programming

With that in mind, I figured the first step was obvioius…dive right in and make a page using HTML5, from the bottom up.  Oh, and create my first site ever using responsive design, can’t forget that.  Hmm…but if I do that, should I write it in ASP.NET?  What about PHP?

In the end, I realized it would be much, much easier to work BACKWARDS from my usual method of diving into something and flailing around until I figure things out.  So, for this site, I’m going to take a “WordPress First” approach.

WordPress is a mature, feature rich blogging platform that has been around since the early days of blogging.  It also runs on PHP, giving me a spot (a subdomain in this case) to play with and learn PHP coding.  It also has thousands upon thousands of rabid fans who have created hundreds of themes, plug-ins, widgets…plus, it’s free.  By downloading and installing WordPress, I can start with a stripped-down responsive, HTML5 theme and work from there to create the site that I want.  And in that spirit, enter Bones.

Bones is a WordPress theme that is left intentionally light.  Built around the brilliant HTML5 Boilerplate (Which is where I had started my original popmatic.com site before I decided to move over to the “WordPress first” strategy) it will give me the backbone I need to move forward with the rest of my site.  The strategy from here will be:

  • Determine a look for Popmatic and recode the Bones template to fit that look
  • Create an ASP.NET master page / default page that can work as the homepage for www.popmatic.com, based upon the look created for the blog
  • Start adding whatever fun tools or upgrades I would like to tackle

The bottom line is, by starting with WordPress, I can get a good, solid foundation to work from, and then move forward from here.  Here we go.