Learn how you can use JavaScript and jQuery to add interactivity to web pages.

Illustration of a man in a tux sitting at a grand piano. Colorful abstract shapes emanate from the piano.

JavaScript is a scripting language and open web standard that is supported by all major browsers. You can use JavaScript to add interactivity and additional functionality to a page. jQuery is a JavaScript library that you can use to add user interface elements, effects, and animations more easily to your web pages. Be sure to download the project files available for this tutorial to follow along with the steps.

Before you start

Download and save the project files. Open step-1-intro-js.html in Dreamweaver, and view the Source Code in Split view.

A file is open in Dreamweaver. The Source Code is selected in Split view.
Download and save the project files

Add a JavaScript alert

One of the best ways to learn JavaScript is to start with the alert function to understand the basic structure of how scripting works. The alert function displays a message in a dialog box. With JavaScript you need to define the function — the action that occurs. Then, you need to define the event that makes it happen. Events are things that happen — such as a user clicking a button or tabbing out of a field — that tell the function when to work.

Add the alert function inside <script></script> tags inside the <head></head> and name it messageWindow. Then, add the onClick event to the button at the bottom of the page to call the display alert when the user clicks it.

HTML in head section

<head>
<script>
function messageWindow() {
  alert("We will show more in Step 2!");
}
</script>
....
</head>

The JavaScript alert function is defined.
Define the alert function

onClick event for button

<div id="moreBtn" onClick="messageWindow()">
SHOW MORE
</div

The onClick event is defined.
Define the event

Preview your changes in a browser

Click File > Preview in Browser and choose your preferred browser to test your JavaScript!

You will need to save your files to view the changes.

Click File > Preview in Browser and choose preferred browser to test JavaScript.
Test your JavaScript in a browser

Introduction to jQuery

jQuery is a JavaScript library that includes pre-built functions that make it easier to add interactivity, animations, and other effects to your page without adding a lot of JavaScript code. When you work in Design View in Dreamweaver, be sure to check out the jQuery widgets available from the Insert > jQuery UI or jQuery Mobile menu options.

You can also add your own jQuery scripts to a page. Check out jqueryui.com on the web to see the other interactions, widgets, and effects you can add.

jQuery widgets are available in the Insert > jQuery UI menu.
Access the jQuery widgets

Open 2nd practice file

Open step-2-intro-js.html in Dreamweaver, and view the Source Code in Split view.

A file is open in Dreamweaver. The Source Code is selected in Split view.
Open the second practice file

Add jQuery animation

Let's add a jQuery function to hide and display a section of content on a page to get an idea of how to use jQuery on a web page. You can download and install jQuery yourself, or you can reference the jQuery library that is hosted by Google or Microsoft. In this example, let's use the library hosted by Google as it is a faster way to include the library and less files to maintain than installing it separately.

Use <script></script> tags inside the <head></head> tags to import the library to your page:

Code is added to reference the jQuery library hosted by Google.
Add a jQuery function

<script src=
"http://ajax.googleapis.com/ajax/
libs/jquery/1.11.2/jquery.min.js"
>
</script>

Then, add the slideToggle function to display and hide the text section on the page when the user clicks the button. In the code below, #moreBtn is the id of the button the user clicks to make the slideToggle function happen. #content3 is the id of the <div> that appears and disappears when the user clicks the button.

<script>
  $(document).ready(function(){
    $("#moreBtn").click(function(){
    $("#content3").slideToggle("slow")
    });
  });
</script>

jQuery and slideToggle functions are added between <script> tags.
Use <script> tags

Preview your changes in a browser

Click File > Preview in Browser and choose your preferred browser to test your JavaScript.

Be sure to save your files in order to view the changes.

Click File > Preview in Browser and choose preferred browser to test JavaScript.
Preview your changes in a browser

Save files to view changes.
Save your files to view the changes

 Adobe

Pridobite pomoč hitreje in preprosteje

Ali ste nov uporabnik?

Adobe MAX 2024

Adobe MAX
Konferenca o ustvarjalnosti

14.–16. oktober v Miami Beachu in spletu

Adobe MAX

Konferenca o ustvarjalnosti

14.–16. oktober v Miami Beachu in spletu

Adobe MAX 2024

Adobe MAX
Konferenca o ustvarjalnosti

14.–16. oktober v Miami Beachu in spletu

Adobe MAX

Konferenca o ustvarjalnosti

14.–16. oktober v Miami Beachu in spletu