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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:58:56+00:00 2026-06-16T04:58:56+00:00

I have a 3×3 HTML table with each element storing a separate <canvas> element

  • 0

I have a 3×3 HTML table with each element storing a separate <canvas> element
There is one cell in the table containing text instead of a <canvas>
When a cell containing an image is clicked and the cell to its right contains text, I’m trying to swap that cell’s canvas with the text stored in the neighbouring cell.
With the following code every time I click a cell, the canvas element disappears and only swaps the text, leaving the cell on the right no longer containing text or a <canvas>
I tried accessing the canvas using ("#blankCell").html() but it does not work .. anybody know how to access this content for swapping of values?

I created a simplified jsFiddle for this: http://jsfiddle.net/bobbyrne01/dbMnM/1/

$(function () {

    var $tbl = $('<table border="1">').attr('id', 'grid');
    var $tbody = $('<tbody>').attr('id', 'tableBody');
    var tileCount = 0;
            var rowCount = $("#numOfPieces").val();

    for (var i = 0; i < rowCount; i++) {

        var trow = $("<tr>").attr('id', 'row' + i); // New row

        for (var j = 0; j < rowCount; j++) {

            $cell.append(canvasArray[tileCount]); // Each data cell contains separate canvas element
            $cell.appendTo(trow); 
            tileCount++;
        }

        trow.appendTo($tbody);
    }

    $tbl.append($tbody);
    $('table').remove(); // removes previous table if it existed
    $('body').append($tbl);
});

$('#grid tr:nth-child(2) td:last').prev().text("empty");
$('#grid tr:nth-child(2) td:last').prev().attr('id', 'blankCell');

Listens for clicks ..

   $('body').on('click', '#grid td', function(e) {

    var $this = $(this);
    if ($(this).closest('td').next("#blankCell").length){

        blank = $(this).closest('td').next("#blankCell");

        if (blank.length) {
            //alert('blank to the right');
            $this.before(blank);
        }
        $(this).attr('id', 'blankCell');
        $(this).closest('td').next("#blankCell").attr('id', 'piece');

    }

Appreciate any help!

Instead of using canvas elements for swapping, it now only has text.
The goal is if a user selects a cell beside the blankCell I would like to swap that cell with the blankCell, if the user selects a cell not beside blankCell I would like nothing to happen.
Only concerned with left and right detection for now.

  • 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-16T04:58:57+00:00Added an answer on June 16, 2026 at 4:58 am

    Make it simple and just exchange the whole table cells, not their contents or content strings and attributes:

    $('#grid').on('click', 'td', function(e) {
        var $this = $(this),
            blank = $this.next("#blankCell");
        if (blank.length) {
            //alert('blank to the right');
            $this.before(blank);
        }
    });
    

    OK, some more complicated code for checking the position included:

    var empty = $("#empty").get(0);
    $('#grid').on('click', 'td', function(e) {
        if (!empty || this == empty) return; // abort, abort!
    
        var currow = this.parentNode,
            emptyrow = empty.parentNode;
        var cx = this.cellIndex,
            cy = currow.rowIndex,
            ex = empty.cellIndex,
            ey = emptyrow.rowIndex;
        if (cx==ex && Math.abs(cy-ey)==1 || cy==ey && Math.abs(cx-ex)==1) {
            // empty and this are next to each other in the grid
            var afterempty = empty.nextSibling,
                afterthis = this.nextSibling;
            currow.insertBefore(empty, afterthis);
            emptyrow.insertBefore(this, afterempty);
        }
    });
    

    Demo at jsfiddle.net

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

Sidebar

Related Questions

i have C++ program exporting log files as HTML table and I wanted to
Have dict like: mydict= {'a':[],'b':[],'c':[],'d':[]} list like: log = [['a',917],['b', 312],['c',303],['d',212],['a',215],['b',212].['c',213],['d',202]] How do i
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
have 2 questions : A computer with 32-bit address uses 2-level page table (9
I have the following html file: <html xmlns=http://www.w3.org/1999/xhtml xml:lang=en dir=ltr lang=en> <head> </head> <body>
I have another one problem with pop3. Here is connection to pop3-server: $pop3Server =
I have a string that is name,5486984,189.658.235.215,3.158.68.15,name2,189189,189.658.218.268,3.158.68.13... and so on I was thinking that
Hi there I have the following matrix [[ 47 43 51 81 54 81
I have a header file in which there is a large struct. I need

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.