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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:34:38+00:00 2026-06-07T12:34:38+00:00

I have an array of associative arrays, i.e. objects, that hold the data of

  • 0

I have an array of “associative arrays,” i.e. objects, that hold the data of various HTML elements and one other parameter like so:

container[0] = { picker_canvas: document.getElementById("background_picker"),
                 color_canvas: document.getElementById("background_color"),
                 hex_text: document.getElementById("background_text"),
                 mouse_down: false };

container[1] = { picker_canvas: document.getElementById("textbox_picker"),
                 color_canvas: document.getElementById("textbox_color"),
                 hex_text: document.getElementById("textbox_text"),
                 mouse_down: false };

container[2] = { picker_canvas: document.getElementById("font_picker"),
                 color_canvas: document.getElementById("font_color"),
                 hex_text: document.getElementById("font_text"),
                 mouse_down: false };

Each container has a reference to a color picker canvas, a color preview canvas, a textbox that displays the hexadecimal color value, and a mouse_down boolean. Later on I initialize a few event listeners by iterating through the containers like this:

for (i=0; i<3; i++) {

    container[i].picker_canvas.addEventListener("mousedown", function() {
        container[i].mouse_down = true;
    }, false);

    container[i].picker_canvas.addEventListener("mouseup", function() {
        container[i].mouse_down = false;
    }, false);

    container[i].picker_canvas.addEventListener("mousemove", function(evt) {
        getColor(container[i], evt);
    }, false);

    container[i].hex_text.addEventListener("change", function(evt) {
        drawColorSquare(container[i], evt.target.value)
    }, false);

}

but that doesn’t work because i becomes undefined, so I am now trying to get the correct index by doing something like this, but I am not sure how to actually implement it in Javascript

container[i].picker_canvas.addEventListener("mousedown", function(evt) {
    container[container.indexof(evt.target)].mouse_down = false;
}, false);

basically I need to search the array of associative arrays for the evt.target and have it return the index of the array. So is this even possible with any built in Javascript functions or will I just have to make my own?

  • 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-07T12:34:39+00:00Added an answer on June 7, 2026 at 12:34 pm

    Since it seems you don’t have to worry about Internet Explorer 8 and earlier, use forEach:

    container.forEach(function(x) {
        x.picker_canvas.addEventListener("mousedown", function() {
            x.mouse_down = true;
        }, false);
    
        x.picker_canvas.addEventListener("mouseup", function() {
            x.mouse_down = false;
        }, false);
    
        x.picker_canvas.addEventListener("mousemove", function(evt) {
            getColor(x, evt);
        }, false);
    
        x.hex_text.addEventListener("change", function(evt) {
            drawColorSquare(x, evt.target.value)
        }, false);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have an associative array of objects that I want to extend with a
Possible Duplicate: Length of Javascript Associative Array I have a JSON that looks like
I have a javascript object that contains a few objects that contain associative arrays.
I have an associative array which when var dumped looks like this: Array (
I have an associative array that I want to display using TileList. However, it
I have an associative array assigned to a Smarty variable, something like this: $foo
I have a PHP array of associative arrays with the following format: array(1) {
I have an Object of Objects. (because I want to use associative array, so
I have an array of php objects that I want to store into a

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.