loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 

C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Javascript page modifiers

by naspinski 7/27/2008 12:23:00 PM

Simple way for your users to modify how a page looks

I have been asked more than once on how to make javascript (or as everyone asks now, 'AJAX') methods to modify a page.  It is very simple and easy to implement.

 

In the supplied example, I show modifiers for both single areas, and the whole page.  I know I chose hideous colors, but live with it, it's just an example...

 

First, what you need to do is some simple JS that allows you to change the style attributes of your html, here are both sets of functions; ones that apply to the whole page, others that apply to specific parts:

 

[code:js]

//apply to the whole page
function fontSize(number)
{ document.body.style.fontSize = number + "em"; }
function fontColor(color)
{ document.body.style.color = color; }
function backgroundColor(color)
{ document.body.style.backgroundColor = color; }

//apply to a specific part of the page
function divFontSize(number, div)
{ document.getElementById(div).style.fontSize = number + "em"; }
function divFontColor(color, div)
{ document.getElementById(div).style.color = color; }
function divBackgroundColor(color, div)
{ document.getElementById(div).style.backgroundColor = color; }

[/code]

 

As you can see,that is about as simple and straight-forward as you get, then simple call them like this:

 

[code:html]<div>    Single Div Modifiers:     <a href="javascript:divFontSize('0.8', 'div1')" style="font-size:0.8em;" >small</a> |     <a href="javascript:divFontSize('1', 'div1')" >medium</a> |     <a href="javascript:divFontSize('1.2', 'div1')" style="font-size:1.2em;">large</a> |    <a href="javascript:divFontColor('black', 'div1')" style="color:Black;">black</a> |     <a href="javascript:divFontColor('navy', 'div1')" style="color:Navy;">navy</a> |     <a href="javascript:divFontColor('green', 'div1')" style="color:Green;">green</a> |     <a href="javascript:divBackgroundColor('white', 'div1')" style="background:white;">White</a> |    <a href="javascript:divBackgroundColor('grey', 'div1')" style="background:grey;">Grey</a> |    <a href="javascript:divBackgroundColor('yellow', 'div1')" style="background:yellow;">Yellow</a></div><div id="div1">    Lorem ipsum dolor sit amet.     Mauris nunc odio, molestie quis.     Sed ac orci eu diam dapibus scelerisque.      Phasellus in nulla a justo dignissim suscipit.     Class aptent taciti inceptos himenaeos.</div>[/code]

 

In the code shown above, the modifiers will only affect whatever is in div1.  And there you have it.

 


 
 
 

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

javascript | steal some code

Awesome JavaScript DateTime Picker

by naspinski 4/30/2008 4:05:00 AM

A great tool that is completely customizable and can do more than your current DateTime picker!

The folks at Zapatec came up with an amazing and free customizable DateTime picker.  You can check out all sorts of demos here, or make your own and use their javascript (no downloads) here.  I love this tool, gonna be using it a lot from now on, thanks Zapatec!  I included an example of how to use it in asp.net.  Also, I have found it easiest to use the wizard on their page to customize it.



Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ajax | javascript | steal some code

Text Input Watermarks using Javascript (IE Compatible)

by naspinski 4/10/2008 8:52:00 AM

Simple way to add watermarks to your textboxes for a little flair in your input forms

I have always used the TextBoxWatermark control that is supplied with the AjaxControlToolkit which is very easy to use and I highly recommend it.  But recently I was asked to figure out a way without using 3rd party tools, hence in JavaScript.  At first I figured it was going to be simple, just clear the text when you click on the box and maybe change the background color.  Simple, that works.   Thats when I ran into my next problem.

 

I wanted a textbox that was forr passwords, it would start out and say 'password' but then when you clicked on it, that would disappear and it would become a type="password" box (display text as: ******).  It worked flawlessly! ...in non-crappy browsers.  IE7 dumped all over my dreams yet again and left me with a Javascript error.  Turns out you can't change the type property in IE7 (apparently it works in IE6???) which is completely stupid.

 

So, I did some good old googling which brought me here to find out that in order to change type, you now have to make an entire new object and swap it out with the old one.  So after that, it works, but my focus is all screwed up: I click on the blank, the password text disappears, but the cursor is not in the textbox I just clicked?!  That brought me here which showed me a crazy way to reset focus on a lost object:

window.tempObject = newObject;
   setTimeout("tempObject.hasFocus=true;tempObject.focus();",1);

Now everything seems to work just fine.  BUT, they aren't.  For one thing, I had had an onblur statement in my textbox originally (not in the example, but for discussion purposes) and since the new object did not have an onblur, I had to make sure to re-add that back in.  Also, the same loss goes for CSS class, but that might want to change, so I added a new field into the function to take into account a new CSS class.  If it is left blank, it will inherit the old CSS class if there is one, or simply have no class like Britney Spears. 

 

Ok, so I *thought* that would be easy, but IE comes back again to spite me.  For most browsers, you can set Object.setAttribute("onblur", "JSmethod") but IE decides that isn't good enough, so you have to to this Object.onblur = function(){js_function(god, damnit, ie);}; (from this thread).  Ok, now that we have that fixed, we can look at the CSS class.  It's mostly the same, but this is yet another IE specific problem.  You need to include two different declarations: Object.setAttribute("class", "css_class"); for most browsers and Object.setAttribute("className", "css_class"); for IE.  This time the IE fix/hack will not work for cross-browser compatibility.  So here is the finished JS:

 

[code:js]

 function makePassword(oldObject, newClass)
{
    //newclass is the new css class to pass to the textbox
    //otherwise leave it blank ('') to inherit it's old class
    //or if there isn't one, have no class
    var newObject = document.createElement('input');
    //only switched to password if the original text was password
    newObject.type = (oldObject.value == 'password')?'password':'text';
    if(oldObject.id) newObject.id = oldObject.id;
    /// this decided whether to keep the old class or get a new one
    var cssClass = (oldObject.className && newClass.length < 1)?oldObject.className:newClass;
    newObject.setAttribute('className', cssClass);// for IE
    newObject.setAttribute('class', cssClass);// for others (these are both needed)
    //the following is hard coded to add onblur functionality to the control
    //newObject.setAttribute('onblur', 'blurred(this)');// this would work if it wasn't for IE
    newObject.onblur = function(){blurred(this,newClass);}; // because of IE
    oldObject.parentNode.replaceChild(newObject,oldObject);//this is necessary because of IE
    //this is needed since a focus() will only work the second time since it happens too 'soon'
    window.tempObject = newObject;
    setTimeout("tempObject.hasFocus=true;tempObject.focus();",1);
}

 

Remember though that you will have to validate that the original values are not in the textboxes when the user submits, as they will not be empty initially.  Here is a fully working example to mess with, it shows how to use it both for a simple watermark/mask and also with a password field change.  It also includes how to add javascript function calls and deal with changing (or persistent) css:


 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

html | javascript | steal some code | tutorials

'Auto-tabbing' to next TextBox with asp.net and Javascript

by naspinski 3/5/2008 5:46:00 AM

If you have a form that limits characters entered into a textfield, using this simple approach you can automatically jump to the next control, making it easy for your user

Say you have a zip code field that takes in 5 numbers, next you have a state dropdown.  Your user should be able to type in the 5 digit zip code, then boom, the focus is automatically shifts to the dropdown.  It is very simple to implement.  It just requires the following JS function:

 

function next(currentControl, maxLength, nextControl)
   {
    if(document.getElementById(currentControl).value.length >= maxLength)
    document.getElementById(nextControl).focus();
  }

 

and then just call this from a textbox in html:

<input id="Text1" onkeyup="next('Text1', '5', 'Text2')" type="text" />

Or if you are using an asp.net TextBox control:

txt1.Attributes.Add("onkeyup", "next('txt1','5','txt2')");

it's just that easy... here is an example using both asp.net and just html:



Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

asp.net | html | javascript | steal some code

HTML/JavaScript Dropdown List Redirect

by naspinski 2/7/2008 12:57:00 AM

Recently on a forum the question was asked how to redirect using a dropdown and not using any server side scripting - the answer turns out to be pretty simple

All that is needded is a simple Javascript function that takes in a drop-down list as an input, takes the value of the selected option and run it through a location.href.  Here is the JavaScript:

function Navigate(drop_down_list) {
   var number = drop_down_list.selectedIndex;
   location.href = drop_down_list.options[number].value; }

Then all you need to do is add a dropdown list setting the values to the urls you want to navigate to, and the onclick event set to Navigate(name_of_dropdown) and you are all ready to go. Here is a quick example:



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

html | javascript