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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:10:21+00:00 2026-06-14T17:10:21+00:00

Sorry about the confusing title, I’ll explain better. I have a 20×20 grid of

  • 0

Sorry about the confusing title, I’ll explain better.
I have a 20×20 grid of div’s, so its 400 of them each with an id, going from 0 to 399.

Each div is given one of three random values – red, green or blue – and when a div is clicked, a function is run to check if the div to the left, right, over and under are of the same value, if it is of the same value it will be simulated a click and the same function will run again.

The problem, is that the function sets vars, so if it finds that the div below has the same value, it will overwrite the vars set by the first click, hence never click any of the others.

JSfiddle – http://jsfiddle.net/5e52s/

Here is what I’ve got:

<!DOCTYPE html>  
<html lang="en">  
<head>
    <meta charset="utf-8"/>
    <title>untiteled</title>
    <style>
        body {
            width: 420px;
        }
        .box {
            width: 19px;
            height: 19px;
            border: 1px solid #fafafa;
            float: left;
        }

        .box:hover {
            border: 1px solid #333;
        }

        .clicked {
            background: #bada55 !important;
        }

    </style>

    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>

        $().ready(function(){
            var colors = ['red', 'green', 'blue'];
            var i = 0;
            while(i<400){
                var color = colors[Math.floor(Math.random() * colors.length)];
                $('.test').append('<div class="box" id="'+i+'" value="'+color+'" style="background:'+color+';">'+i+'</div>');
                i++;
            }

            $('.box').click(function(){
                var t = $(this);
                t.addClass('clicked');
                id = t.attr('id');
                val = t.attr('value');

                //Set color

                up = parseInt(id) - 20;
                right = parseInt(id) + 1;
                down = parseInt(id) + 20;
                left = parseInt(id) - 1;
                clickup = false;
                clickdown = false;
                if($('#'+down).attr('value') === val){
                    clickdown = true;
                }
                if(up > -1 && ($('#'+up).attr('value') === val)){
                    clickup = true;
                }

                if(clickdown == true){
                    $('#'+down).click();
                }
                if(clickup == true){
                    $('#'+up).click();
                }







            });
        });

    </script>
</head>
<body>
    <div class="test">

    </div>      
</body>

  • 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-14T17:10:22+00:00Added an answer on June 14, 2026 at 5:10 pm

    I think the biggest root cause of your problem is you don’t check if it already has class ‘clicked’ or not. That could make the infinite recursive. For example, if you click on the div#2 then the div#1 receives a simulated click, and div#2 receives a simulated click from div#1.

    $('.box').click(function(){
        var t = $(this);
        if(t.hasClass('clicked')) {
            return;
        }
    
        t.addClass('clicked');
        var id = t.attr('id');
        var val = t.attr('value');
    
        //Set color
    
        var up = parseInt(id) - 20;
        var right = (id%20 != 19) ? ((0|id) + 1) : 'nothing' ;
        var down = parseInt(id) + 20;
        var left = (id%20 != 0) ? ((0|id) - 1) : 'nothing';
    
        console.log(up, right, down, left);
    
        if($('#'+down).attr('value') === val) {
           $('#'+down).click();                    
        }
        if($('#'+right).attr('value') === val) {
           $('#'+right).click();                    
        } 
        if($('#'+up).attr('value') === val) {
           $('#'+up).click();                    
        }
        if($('#'+left).attr('value') === val) {
           $('#'+left).click();                    
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the confusing title, I am newbie and have no idea what to
Sorry about that confusing title :) I have a resource, ComatosePage (used in the
Sorry about the title, I couldn't think of a better way to describe the
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the wording for my question title. I have a basic HTML anchor
sorry about input mistakes, english its not my mother lang. I have this code
Sorry about the confusing title, I had a hard time describing the problem. I
Sorry about the confusing title. Are there any web IDEs available that can be
Sorry about the confusing title. I recently started doing this in my project, and
Sorry for the confusing title of the question. I am uncertain about how should

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.