Bernard Aybouts - Blog - Miltonmarketing.com

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:

Copy to Clipboard

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.

Copy to Clipboard

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

Copy to Clipboard

Explanation:

  • return false: The return 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.

Copy to Clipboard

Step 2: Create a hyperlink to Google

In the <body>, add an anchor (<a>) tag that links to Google:

Copy to Clipboard

Step 3: Add an onclick alert

Now, modify the anchor tag to show an alert when clicked:

Copy to Clipboard

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:

Copy to Clipboard

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 the onclick attribute.
  • You can stop a link from working using return false in your onclick code.

By mastering the onclick attribute, you can make your web pages more interactive and dynamic!


6. Practice Exercise

  1. Create a button with the text “Show Alert” that pops up an alert when clicked.
  2. Add a second hyperlink that doesn’t navigate to any page when clicked (using return false).
  3. Make a third clickable image that displays a custom message when clicked.

Related Links:

For privacy reasons YouTube needs your permission to be loaded. For more details, please see our Privacy Policy – Legal Disclaimer – Site Content Policy.

The Longevity Blueprint: AI-Powered Health Optimization

Current step:1AI-Human Medical Analyzer: Smarter, Personalized Health
2AI-Human Medical Analyzer: Smarter, Personalized Health

> SYS.HEALTH: AI-Human Medical Analyzer_

// Revolutionize Your Diagnostics

Experience the perfect blend of cutting-edge AI precision and expert human care. Our revolutionary analyzer turns your raw health data into personalized, actionable insights tailored just for you.

> INITIALIZING_BIOMETRIC_SCAN...

[+] DATA_INPUT

Securely upload complex health parameters, including lab bloodwork and comprehensive medical history.

[+] PROCESSING

Advanced algorithmic parsing combined with human-level oversight ensures hyper-accurate data interpretation.

[+] OUTPUT_MATRIX

Receive smarter, faster, and truly personalized care strategies to take immediate charge of your health journey.

A name/nickname is required to continue.

> TRANSLATION_MATRIX_ACTIVE...
[ LANG_EN ]
Knowledge Heals, Prevention Protects
[ LANG_HI ]
ज्ञान ठीक करता है, रोकथाम सुरक्षा करती है
[ LANG_ZH ]
知识治愈,预防保护
[ LANG_JA ]
知識は癒し、予防は守る
[ LANG_HE ]
הידע מרפא, המניעה מגנה
[ LANG_AR ]
المعرفة تُشفي، والوقاية تحمي
[ LANG_FR ]
La connaissance guérit, la prévention protège

> SYS.AUTH: Data Processing Consent_

[ AWAITING_AUTHORIZATION ] By providing consent, you allow us to process your uploaded data through our proprietary AI-Human analysis system.

  • [+] SECURE_REVIEW: This ensures your information is carefully reviewed using advanced AI technology and certified professional oversight to deliver personalized health insights.
  • [+] PRIVACY_LOCK: Your privacy is our strict priority. Your data will only be used for this specific diagnostic purpose.

> SYS.UPLOAD: Share Medical Records [OPTIONAL]_

[ USER_CONTROL_ACTIVE ] Uploading your medical records during registration is entirely optional. You can choose to bypass this step and provide data later if it suits your timeline.

You dictate the data flow: share as much or as little as you’re comfortable with, and let us guide you toward better health.

[+] FORMAT_SUPPORT

We accept all file formats, including photos, PDFs, text documents, and raw official medical data.

[+] DATA_YIELD

Increased inputs correlate with higher precision. The more info you share, the better we tailor your personalized insights.

> NEXT_STEPS: Post-Registration Protocol_

Once your registration is complete, a human specialist from our team will personally reach out to you within 3-10 business days. We will discuss your health journey and map out exactly how we can support you.

About the Author: Bernard Aybout (Virii8)

Avatar Of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀