2

I'm trying to get one random row from an array which is composed of 100 rows. Some are hidden, other are visible.

My aim is to get a random visible row, and clone it in another table. (visible and hidden row are classified, that's why I need that !)

My code:

<table id="Random">
</table>

<table id="Classified">
    <tr id="Row1" style="display:none">
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>

    <tr id="Row2">
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
    </tr>

    <tr id="Row3" style="display:none">
        <td>9</td>
        <td>10</td>
        <td>11</td>
        <td>12</td>
    </tr>

    <tr id="Row4">
        <td>13</td>
        <td>21</td>
        <td>32</td>
        <td>43</td>
    </tr>

    <tr id="Row5">
        <td>15</td>
        <td>26</td>
        <td>37</td>
        <td>48</td>
    </tr>
</table>    


<script>
    $("#Random").html("");
    var randomtd = Math.floor(Math.random() * $('#Classified tr:visible').length) + 1;
    var identifiedRow = $('#Classified tr').eq(randomtd)[0];

    $("#Random").html(identifiedRow);    
</script>
2
  • 1
    Is there any issue with the current code you have? Commented Sep 21, 2017 at 14:38
  • For now, it will take the lenght of every td... So sometime I can clone a visible td, and other time, it will clone an hidden td. Commented Sep 21, 2017 at 14:45

3 Answers 3

1

Here you go with a solution https://jsfiddle.net/1hok7xme/

$("#Random").html("");
   
$('#Classified tr').each(function(){
  if($(this).is(':visible')){
    $("#Random").append(`<tr>${$(this).html()}</tr>`);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Random Table:
<table id="Random">
</table>

<br/>
Classified Table:
<table id="Classified">
    <tr id="Row1" style="display:none">
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>

    <tr id="Row2">
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
    </tr>

    <tr id="Row3" style="display:none">
        <td>9</td>
        <td>10</td>
        <td>11</td>
        <td>12</td>
    </tr>

    <tr id="Row4">
        <td>13</td>
        <td>21</td>
        <td>32</td>
        <td>43</td>
    </tr>

    <tr id="Row5">
        <td>15</td>
        <td>26</td>
        <td>37</td>
        <td>48</td>
    </tr>
</table>

Hope this will help you.

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

1 Comment

This also works. But it takes every visible rows. With my code, I'm trying to random one. :o
0

Your issue is in these lines:

var randomtd = Math.floor(Math.random() * $('#Classified tr:visible').length)+1;
var identifiedRow = $('#Classified tr').eq(randomtd)[0];

Change them to:

var randomtd = Math.floor(Math.random() * $('#Classified tr:visible').length);
var identifiedRow = $('#Classified tr:visible').eq(randomtd)[0];

jQuery .eq() requires an integer indicating the 0-based position of the element.

$("#Random").html("");
var randomtd = Math.floor(Math.random() * $('#Classified tr:visible').length);
var identifiedRow = $('#Classified tr:visible').eq(randomtd);

//$("#Random").html(identifiedRow);
$("#Random").append(identifiedRow.clone());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Random table: <br/>
<table id="Random">
</table>
Classified table: <br/>
<table id="Classified">
    <tr id="Row1" style="display:none">
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>

    <tr id="Row2">
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
    </tr>

    <tr id="Row3" style="display:none">
        <td>9</td>
        <td>10</td>
        <td>11</td>
        <td>12</td>
    </tr>

    <tr id="Row4">
        <td>13</td>
        <td>21</td>
        <td>32</td>
        <td>43</td>
    </tr>

    <tr id="Row5">
        <td>15</td>
        <td>26</td>
        <td>37</td>
        <td>48</td>
    </tr>
</table>

2 Comments

That's work, but it seems to cut/paste the line instead of copy/paste. Maybe with clone ? $("#Random").clone().append(identifiedRow); ?
@OverSu If you don't want to cut/paste but copy/paste the row: $("#Random").append(identifiedRow.clone());. Snippet updated. Let me know.
0

Ok, got it ! Thanks to everyone. Each+Array. ;)

        var arrayTmp = [];
            var i = 0;
            $('.Monstres tbody tr').each(function(){
              if($(this).is(':visible')){
                arrayTmp[i] = '<tr>'+$(this).html()+'</tr>';
                i++;
              }
            });
            var arrayTmp = arrayTmp[Math.floor(Math.random()*arrayTmp.length)];
            $(".Random table").append(arrayTmp);

Comments

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.