Approx. read time: 3.2 min.
Post: Making javascript work with HTML
Lesson: Making JavaScript Work with HTML Using the onclick
Attribute
Introduction
In this lesson, we will learn how to make your web page interactive by using JavaScript in combination with HTML elements. We will focus on using the onclick
attribute, which allows JavaScript to run when a user clicks on an HTML element like a link or a button.
By the end of this lesson, you’ll know how to:
- Add JavaScript code directly to HTML elements using the
onclick
attribute. - Use
return false
to prevent default browser behavior, like following a hyperlink.
1. Adding JavaScript to HTML using the onclick
Attribute
Basic Example
The onclick
attribute is added to an HTML tag, and when the user clicks on that element, the JavaScript code inside onclick
will execute.
Here’s how to create a clickable piece of text that shows a popup message:
Explanation:
- HTML element: The
<p>
tag represents a paragraph in the HTML document. - onclick attribute: We add the
onclick
attribute to the opening tag of the<p>
element. - JavaScript action: Inside the
onclick
attribute, we place JavaScript code that runs when the user clicks on the paragraph. In this example, it shows a popup (alert
) with a message.
2. Using onclick
with Hyperlinks
You can use the onclick
attribute with hyperlinks to run JavaScript when a user clicks a link.
In this example:
- When the user clicks on the link, an alert will pop up saying, “You will be redirected.”
- After dismissing the alert, the user is taken to the website.
3. Preventing the Default Action with return false
Sometimes, you might want to stop the default action (e.g., navigating to another page) after clicking a link. This can be done using return false
inside the onclick
attribute.
Example: Preventing Link Navigation
Explanation:
return false
: Thereturn false
statement prevents the default action (navigating to Google) from happening.- The link becomes unclickable for navigation, but the JavaScript alert will still appear.
4. Step-by-Step Exercise
Step 1: Create a new HTML file
Open a text editor and create a new file named onclick.html
. Add the basic HTML structure to the file.
Step 2: Create a hyperlink to Google
In the <body>
, add an anchor (<a>
) tag that links to Google:
Step 3: Add an onclick
alert
Now, modify the anchor tag to show an alert when clicked:
Step 4: Test the code
Save your file and open it in a web browser. When you click the link, an alert will appear with the message. After clicking “OK,” you will be redirected to Google.
Step 5: Prevent the default behavior
Next, modify the onclick
attribute to stop the link from working:
Save your file and refresh the page. Now, when you click the link, the alert will pop up, but you won’t be taken to Google.
5. Summary
- The
onclick
attribute allows you to run JavaScript code when a user clicks on an HTML element. - Use single quotes (
'
) for JavaScript strings inside double quotes ("
), which enclose theonclick
attribute. - You can stop a link from working using
return false
in youronclick
code.
By mastering the onclick
attribute, you can make your web pages more interactive and dynamic!
6. Practice Exercise
- Create a button with the text “Show Alert” that pops up an alert when clicked.
- Add a second hyperlink that doesn’t navigate to any page when clicked (using
return false
). - Make a third clickable image that displays a custom message when clicked.