0

I have this HTML list and input:

            <li>
                <span>19</span>
            </li>
            <li>
                <span>20</span>
            </li>
            <li>
                <span>21</span>
            </li>
            <li>
                <span>22</span>
            </li>
            <li>
                <span>23</span>
            </li>
            <li>
                <span>24</span>
            </li>
        </ul>
    <input type="text" id='lotto' value='' readonly>
</div>

and this external JavaScript code to get the value of each span I click on in an array

let clicks = 0
let numbers = []

$(document).on('click', 'li', function(event) {
  if (clicks < 6) {
    numbers[clicks] = parseInt($(this).find('span').html())
  }
  console.log(numbers)
  clicks++
})

is there any possible way to display the spans i clicked on in the input ??

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

Yes, it's fairly straightforward. You can use join to turn the array of numbers into a string and then set that as the textbox's value.

Demo:

let clicks = 0
let numbers = []

$(document).on('click', 'li', function(event) {
  if (clicks < 6) {
    numbers[clicks] = parseInt($(this).find('span').html())
  }
  //console.log(numbers)
  clicks++;
  
  $("#lotto").val(numbers.join(","));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li>
  <span>19</span>
</li>
<li>
  <span>20</span>
</li>
<li>
  <span>21</span>
</li>
<li>
  <span>22</span>
</li>
<li>
  <span>23</span>
</li>
<li>
  <span>24</span>
</li>
</ul>
<input type="text" id='lotto' value='' readonly />
ADyson
  • 57,178
  • 14
  • 51
  • 63