1

Here is my jfiddle: http://jsfiddle.net/D3zyt/8/

html:

<div class="board">
 <table id="mastermind_table_one">
    <td></td>
    <td></td>
    <td></td>
    <td></td>
 </table>

 <table id="mastermind_table_two">
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</table>

 <table id="mastermind_table_three">
    <td></td>
    <td></td>
    <td></td>
    <td></td>
 </table>

You'll notice in the html that I have three tables. Is there a way when I click "next_round", the background colors change for the next table and not the current (hardcoded) table?

2
  • I'd store the already coloured table's ID in localSession and then iterate over that data. Once you've coloured all 3 tables you can clear it and repeat the cycle? Commented Apr 24, 2014 at 19:43
  • Use a common class for all tables, store an index variable, and use .eq(#) to select the current table. Better yet, use one table and use .eq(#) to select the desired row. Commented Apr 24, 2014 at 19:44

4 Answers 4

2

This does it by storing the current table in a variable and using .next() to find the next table:

Fiddle

var current;

$('.next_round').click(function() {

   if(typeof current == 'undefined' || current.next('table').length == 0){
       current = $('.board table').first();   
   } else {
       current = current.next('table'); 
   }

   $(current).find('td').each(function() {
     $(this).css("background-color", setRandomColor);
   });

});
Sign up to request clarification or add additional context in comments.

2 Comments

Remove $(current).each(function() {
genius! Thanks Mr.Code. This worked actually like I was hoping for. Thanks a ton!
1

Something like this helps?

var tables = $('.board table');
var currentTable = 0;

$('.next_round').click(function() {
    var table = tables[currentTable];

    table.find('td').each(function() {
        $(this).css("background-color", setRandomColor);
      });

    currentTable++;
    if(currentTable > tables.length){
       currentTable = 0;
    }
}

1 Comment

table.each not required
1

This is a solution that implements event data in jquery.

And here is a fiddle: http://jsfiddle.net/D3zyt/10/

var randomColor = ["red", "blue", "green", "#9CBA7F", "yellow", "#BF5FFF"];

function setRandomColor() {
    return randomColor[Math.floor(Math.random() * randomColor.length)];
}


$('.next_round').on("click", {i: 0}, function(e) {
    var selectorFragment = ["one","two","three"]

    $('#mastermind_table_'+selectorFragment[e.data.i]).each(function() {
      $(this).find('td').each(function() {
        $(this).css("background-color", setRandomColor);
      })
    })

    e.data.i += 1
    if (e.data.i === 3) e.data.i = 0 
})

However, restructuring your html would probably make for an easier solution later down the road ;)

Comments

1

Note: this post contains a bad practice, I left it maybe someone could learn from it, read the comment

just use one table like:

<table id="mastermind_table_three">
<td></td>
<td></td>
<td></td>
<td></td>
</table>

and then add a button <button onclick="nextRound(this) />

with the function as:

function nextRound(that) {
that.i = that.i ? (that.i + 1) : 1; 
$('table').removeClass("mastermind_table_" + that.i - 1);
$('table').addClass("mastermind_table_" + that.i);
}

3 Comments

Warning... this is mixing js with html, bad practise example. Besides that it won't work because you have a '-' inside your function name :O
you're correct about the dash thing, it was a mistake, but mixing html with javascript ?! this is new to me?! isn't DOM manipulation the main reason for using jQuery ?!
You could manipulate DOM and do every other operation without polluting HTML with javascript names. When your application will evolve it will be hard to maintain such code. I'm talking about <button onclick="nextRound(this)"> of course

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.