Using Buttons for javascripts

Using Buttons for JavaScriptsTo write scripts using buttons, we will first need to know how to place a button on the page. To do this, you will need to use the <FORM> tags around the button tag. Here is an example of the HTML that will place a button on the page:

<FORM>
<INPUT type="button" value="Click Me" name="button1">
</FORM>

See the example.

Here is what all this means:

  1. <FORM>
    This creates a form so we can use a button.

  2. <INPUT>
    This tag allows us to create an input area of some kind.

  3. type="button"
    This command declares our input area to be a button.

  4. value="Click Me"
    This will be the text people will see on the button. Write whatever you want your visitors to see.

  5. name="button1"
    You can give the button a name for future reference and possibly for use in a script.

Now, I know you don't just want to make buttons that don't do anything, so let's look at a javascript command that will make the browser do something when a viewer clicks it:

onClick="javascript commands"

Just place this command inside the INPUT tag you created the button with, like this:

<INPUT type="button" value="Click Me" name="button1" onClick=" ">

Now, if you read the last section, you saw how to add text to the status bar using the onMouseover command. Well, you can also use a button to do this!

<FORM>
<INPUT type="button" value="See Some Text" name="button2" onClick="window.status='You clicked the button!'; return true">
</FORM>

See the example.

You can also allow your viewers to change the background color of your page. Just use the following command, rather than the window.status command:

document.bgColor='color'

Just insert this as your instructions for the onClick command, like this:

<FORM>
<INPUT type="button" value="Change to Yellow!" name="button3" onClick="document.bgColor='yellow'">
</FORM>

See the example.

You can add as many of these buttons as you like, just be sure they have the option to change back to the original color. The next script will give you three choices: yellow, red, and original color.

<FORM>
<INPUT type="button" value="Change to Yellow!" name="button3" onClick="document.bgColor='yellow'"> <br>
<INPUT type="button" value="Change to Red!" name="button4" onClick="document.bgColor='red'"> <br>
<INPUT type="button" value="Change back!" name="button5" onClick="document.bgColor='white'"> </FORM>

See the example.

The last script we will do in this section will allow you to use your button as a link. It's pretty fun to use every now and then. Just use the following command in your onClick command:

window.location='url'

Here is a script that will send you to a page I made just for this example:

<FORM>
<INPUT type="button" value="Go to my other Page!" name="button6" onClick="window.location='newpage.htm'">
</FORM>

See the example.


Close    To Top
  • Prev Article-Web Design:
  • Next Article-Web Design:
  • Now: Tutorial for Web and Software Design > Web Design > JavaScript > Web Design Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction