Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8168647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:35:46+00:00 2026-06-06T20:35:46+00:00

The textbook I’m using to learn JavaScript uses the following code to display an

  • 0

The textbook I’m using to learn JavaScript uses the following code to display an alert dialogue whenever a user clicks on a paragraph:

var paras = document.getElementsByTagName("p");
for (var i=0; i<paras.length; i++) {
 paras[i].onclick = function() {
  alert("You clicked on a paragraph.");
 }
}

I don’t see the reason to loop through all the p elements, but instead identify them and simply attach the onclick event handler to it. Like this:

var paras = document.getElementByTagName('p');
paras.onclick = alert("You clicked on a paragraph."); 

Doesn’t that do the same thing? Why is it necessary to loop through the p elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:35:48+00:00Added an answer on June 6, 2026 at 8:35 pm

    No, that absolutely does not do the same thing:

    1. You simply cannot attach event handlers to an HTML node list (well, you can, but nothing will happen);
    2. You’re trying to set the “onclick” to the result of the alert() statement, not a function as in your example. (Doesn’t really matter because it won’t work anyway.)

    Now there is a way to handle the clicks with just one event handler, but I’ll let you keep reading your book 🙂

    edit — To elaborate on point 2, this:

    alert("hi");
    

    is a function call. Its value will be whatever is returned from calling that function. Thus,

    paras.onclick = alert("You clicked on a paragraph.");
    

    sets the “onclick” property of the object that “paras” refers to, and it sets it to the value returned from alert() (which is probably always undefined).

    As in the sample code from your book, things like “onclick” handlers need to be functions. That’s what’s going on in the middle of your sample code: the “onclick” property of each individual <p> DOM element is being set to a function. Inside that function is the call to alert().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some html/css/javascript code that was taken verbatim from a javascript textbook. For
I have the following Spring code, taken from a textbook, that's returning a NotWritablePropertyException.
Using the following textbook thread wait/notify example, is there a tool (Eclipse plugin?) that
I'm following along in my C# textbook and it says that C# assumes that
I'm following this awful textbook, going through the basics of create/edit/delete records. The delete
I'm looking at code in the textbook: Programming With POSIX Threads by David R.
So I was looking at this code from a textbook: for (int i=0; i<N;
On CLRS's textbook Introduction to Algorithm, there's such paragraph on pg. 258. We can
Every textbook I've ever seen on recursive functions uses the factorials as an example,
From Java Malik textbook- determine if an number is divisible by 11.. Code Solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.