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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:16:55+00:00 2026-05-25T11:16:55+00:00

I’m looking for a way to make a grid (for now using Table, but

  • 0

I’m looking for a way to make a grid (for now using Table, but soon in div).
Let’s sais I click on the first cell (x,y = 1,-1) I want the background color to change for 2 cells width and 3 cells height. (Total of 6 cells changed)…

If it’s easier to do it using div, go ahead… using jQuery please! 🙂

I really don’t know how to do this and if someone can put me on the path or give me a code that should do it… or better, a tutorial XD…

I really appreciate your help, 100 times thanks

EDIT:

What i’m trying to do actually here is an invisible grod make a system comparable to rts-like games, where the building is transparent and follow the mouse but it’s attached to the grid when you move, and on clikc the bulding is droped (no transparency)… Explaining this just so you can have a little visual here.

  • 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-05-25T11:16:55+00:00Added an answer on May 25, 2026 at 11:16 am

    The following is for div (as it’s the long term goal):

    Firstly, I made a little markup that will be like a sort of table.

    The html is:

    <div id="overall">
        <div class="row">
            <div class="cell col0"></div><div class="cell col1"></div><div class="cell col2"></div><div class="cell col3"></div>   
        </div>
        <div class="row">
            <div class="cell col0"></div><div class="cell col1"></div><div class="cell col2"></div><div class="cell col3"></div>
        </div>
        <div class="row">
            <div class="cell col0"></div><div class="cell col1"></div><div class="cell col2"></div><div class="cell col3"></div>       
        </div>
        <div class="row">
            <div class="cell col0"></div><div class="cell col1"></div><div class="cell col2"></div><div class="cell col3"></div>    
        </div>   
    </div>
    

    With the following css:

    .row{
        height:25px;
    }
    .cell{
        width:25px;
        height:100%;
        display:inline-block;
        border:1px solid black;
    }
    

    So it has a table like display (it’s a bunch of row composed of cells, cells in a same column share a class).

    To do what you want, it looks like you’ll need to associate a click function to each .cell.

    To do so is easy, using $(".cell").click(function(){});

    Now, it’s time to complete the function.

    Now the next step is coloring the cell you’ll need to color. To add the coloration we’ll use a special class (this way we can change more things easily):

    .clickedCell{
        background:red;
    }
    

    The hardest part is to select the 6 cells. Numerous way can be used (for example we could have a grid of id like A1, A2, B1, B2 and select them using id) and the efficiency/design depends heavily on the markup you’ll have for your divs.

    The way I’d do that is the following:

    • Retrieving the class of the column of my cell :

      var cl=$(this).attr("class");
      var col="."+/col\d/.exec(cl)[0];
      
    • Retrieving the parent of current div var parent=$(this).parent();

    • Making a jQuery object containing the 3 cells in the current colomn:

      listOfCell=$(this);
      listOfCell=listOfCell.add(parent.next().children(col));
      listOfCell=listOfCell.add(parent.prev().children(col));
      

      Note that the add function returns a new collection, thus we need to assign the return value.

    • Adding the 3 next cells to that object listOfCell=listOfCell.add(listOfCell.next());

    • Adding the class listOfCell.addClass("clickedCell");

    And it’s over 🙂

    A working example here: http://jsfiddle.net/KZFzd/1/

    Note that:

    • As said before, the function depends heavily on the markup used.
    • The example does not handle the deletion of previously selected cells. It’s easy and left as an exercise to the reader.
    • It does not handle the special case of the cells on the side, it just change the background of the cell that would be changed if the grid was greater. This case is left as an exercise to the reader.
    • It does not check the existence of next/previous parents because jQuery returns an empty jQuery object when nothing matches, and therefore, methods can be called on it, even if it has no effects.
    • The example can be compacted in many ways, but is left as is for readability purposes.

    Hope that helps.

    EDIT:
    In order to answer to your comment, the new fiddle to handle a specified size: http://jsfiddle.net/KZFzd/3/

    I added two input that let you specify the size. You’ll probably need to change that in your code :).

    I also added the class removal to clean the display.

    So the two main change are that now, we’re using two for loops to add cells. And the clicked cell is the top left corner of the rectangle.

    • the first one:

      for (i=1;i<y;i++){
         listOfCell=listOfCell.add(par.children(col));
         par=par.next();
      }
      

      It’s just iterating from one parent to another to reach the desired height. (and the first parent assignation is now the next one directly. par=$(this).parent().next()

    • the second one:

      for (i=1;i<x;i++){
          listOfCell=listOfCell.add(listOfCell.next());
      }
      

      It’s just iterating to add the next elements to reach the desired width.

    Note that:

    • We’re using the fact that there are no double in a jQuery list in the width.
    • We’re iterating starting from 1 and not 0, because our starting listOfCell is already a 1*1 cell
    • You can easily start from others corner if you change the use of next() to prev() in one loop or/and the other.
    • It still does not handle the side cases.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.