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 8540879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:41:58+00:00 2026-06-11T11:41:58+00:00

For example we have Events in HTML like this: <div onclick=bla(this) class=gg> How could

  • 0

For example we have Events in HTML like this:

<div onclick="bla(this)" class="gg">

How could we add an Event Listener/Handler to all the gg elements by not writing the onclick="bla(this)" thing in HTML?

I’ve tried something like this. But of course it does not work.

var ggs = document.getElementsByClassName('gg');
for (var i = 0; i < ggs.length; i++) {
// the 'this' below needs to go somewhere to become working
    ggs[i].addEventListener('click',bla,this); 
}

EDIT: The original code works http://jsfiddle.net/pVpaV/2/ . To make the code to work I need to put it at the end of the page.

  • 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-11T11:41:59+00:00Added an answer on June 11, 2026 at 11:41 am

    If you need to keep using bla as it was originally, you’ll need to wrap it and pass the appropriate parameter:

    var ggs,
        i;
    ggs = document.getElementsByClassName('gg');
    for (i = 0; i < ggs.length; i++) {
        ggs[i].addEventListener('click', function (e) {
            //in the event handler, `this` refers to the clicked element
            bla(this);
        }, false);
    }
    

    If you simply need to call bla in the context of the element that triggered the event, addEventListener already does that by default.

    var ggs,
        i;
    function bla(e) {
        //`e` is event data
        //`this` is the element that triggered the event
    }
    ggs = document.getElementsByClassName('gg');
    for (i = 0; i < ggs.length; i++) {
        ggs[i].addEventListener('click', bla, false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I got something like this: HTML: <div id="main" class="underline" >Hello</div> JavaScript: $('div').live('click', function(){
Example: I have a selector like this, which I give to another method as
Currently my AJAX is working like this: index.php <a href='one.php' class='ajax'>One</a> <div id=workspace>workspace</div> one.php
How can I have additional data on an HTML element? For example, I'd like
I have an website with URL`s like example.com/index.php?p=home An this script $(document).ready(function() { var
I have the following Javascript/jQuery function: function addEventHandler(){ $(div).mouseenter(function() { $(this).html(Over); }).mouseleave(function() { $(this).html(Out);
Take for example a html like the following: <html> <head> <title></title> </head> <body> <div>
I have this project i'm working on and id like to add a really
If I for example have <p> some long text </p> on my HTML page,
Example: I have an class that inherits from UIImageView. An object creates an instance

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.