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

  • SEARCH
  • Home
  • 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 8192447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:11:01+00:00 2026-06-07T04:11:01+00:00

I have a button in my project that when you click over it a

  • 0

I have a button in my project that when you click over it a function call and add onclick event to all certain elements in my project and show my hidden popup element container.

I have a function that search all exist element in my page and add onclick event to some of elements that they have certain class.

My element is stored in a list array. in each cell of this array (array name is list) stored an element like below:

list[0] = document.getElementById("my_div_id");
list[1] = document.getElementById("my_div_id_1");
list[2] = document.getElementById("my_div_id_2");
...
list[n] = document.getElementById("my_div_id_n");

and I have a function like below in top of my Javascript code:

function say_hello(e, msg) {
    if (e == null) e = window.event;
    //now e handler mouse event in all browser !!!
    alert (e + "::" + msg);
}

I have a function to add onclick event to each element in array. I add onclick event in type of below (separated with (*) comment) but doesn’t work any of them:

function search_and_add_events_to_all_dragable_elements (list) {
    for (var z = 0; z < list.length; z++) {
        list[z].href = "javascript:;";
        
        var e;
        var test_msg = "VAYYYYYYYYYY";
        
        /**************
        element.onclick = new Function { alert ('hi'); };
        element.onclick = new Function () { alert ('hi'); };
        element.onclick = new function { alert ('hi'); };
        element.onclick = new function () { alert ('hi'); };
        element.onclick = new function () { return alert ('hi'); };
        element.onclick = function () { return alert ('hi'); };
        element.onclick = alert ('hi');
        element.onclick = "alert ('hi');";
        element.onclick = say_hello(e, test_msg);
        element.onclick = "say_hello();";
        element.onclick = (function (e, test_msg) { return function(e) { sib(e, test_msg); };
        element.onclick = (function () { return function() { alert("ahaaay"); };
        **************/
        
        list[z].style["padding"] = "20px";
        list[z].style["border"] = "solid 10px";
        list[z].style["backgroundColor"] = "#CCC";
    }
}

I change style in end of my code to perform my code is work and end truly. style change every time but onclick event doesn’t add to my div.
only one way add onclick to my project. that is same as below:

list[z].setAttribute("onclick", "alert(\"hi\");");

but are there better ways?

  • 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-07T04:11:02+00:00Added an answer on June 7, 2026 at 4:11 am

    There is a better way. My first mistake was using JavaScript before my all element load on my page. to solve it you must call element in end of page load or put your javascript code in end of your project. then your code execute exactly when your elements are exist in your page.
    for more details about it see links below:
    JavaScript that executes after page load

    http://www.w3schools.com/jsref/event_onload.asp

    My second mistake was hurt 🙁
    I has a div that hold all of my other elements in itself. it was styled display: none; on load. when I call my function it was displayed none and all thins work well (like my new styling) but onclick event didn’t work :(( and I spent two days to solve this :((
    only be careful your element should not be display: none styled when you are adding your onclick event to it.
    then you can use this type of creation onclick event dynamically to your project:

    list[z].onclick = (function (e, test_msg) {
        return function(e) {
            sib(e, test_msg);
        };
    })(e, test_msg);
    

    this is best way that I know. you can manage event handler and send your arguments also to your function.

    I use several time another way of dynamically add onclick event in my project.

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

Sidebar

Related Questions

In my Delphi Project i want to have a 'Settings' button that when clicked,
I have button, which fires an event, that deletes a record from the database.
I have a form that shows a project and all the systems associated to
I have a VB6 project with over 300 forms. I want to remove all
In my project i have a button. when user clicks on it, it shows
I have one issue with UInavigationcontroller In my firstview i have button.If i click
I have a button that when clicked will run a stored procedure on a
In my Flash project I have a movieclip that has 2 keyframes. Both frames
I have an MVC2 project that has a page on it that is supposed
I have a visual studio 2010 web project that when I publish it in

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.