JavaScript Getting [object HTMLLIElement] instead of text. Javascript function

Hello I have a simple javascript function that chooses a random list item in an unordered list with the press of a button. It works fine, the console spits out a random list item when the button is clicked but when I try to display it on the HTML page all I am getting is this weird [object HTMLLIElement] instead of the text.

The goal is to display the text inbetween the list item. When I click the button it chooses a random list item for example: <li>5</li> but I would like for just the 5 to be displayed.

Here is the HTML:

<ul id="list">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
</ul>

<button id="randomize">randomize</button>

<p id="result">hello</p>

and the JS:

var randomize = document.getElementById("randomize");
var listItems = document.getElementById("list").getElementsByTagName("li");
var result = document.getElementById("result");

randomize.addEventListener("click", randomizeIt);

function randomizeIt () {
    var randomItem = listItems[Math.floor(Math.random() * listItems.length)];
    result.innerHTML = randomItem;
    console.log(randomItem);
}

thanks in advance

Answer:1

Well you set result to the li object, you have to get the text inside

result.innerHTML = randomItem.textContent;
Answer:2

I am making a Space Invaders clone and I am almost done, but I am having issues with my Space Invaders moving. I have tried using += to move the Space Invaders but this still isn't working. Here is my ...

I am making a Space Invaders clone and I am almost done, but I am having issues with my Space Invaders moving. I have tried using += to move the Space Invaders but this still isn't working. Here is my ...

  1. arena
  2. arena football
  3. arendelle
  4. arena stage
  5. arenado
  6. arenal volcano
  7. arena sports
  8. arent
  9. arenal costa rica
  10. arena theatre
  11. arena of valor
  12. arena stage dc
  13. arena sf
  14. arent fox
  15. arena ktown
  16. arenanet
  17. arenado trade
  18. arenacross
  19. arenal volcano national park
  20. arena theater

If I create a sparse array (which has some holes in it), the array functions, like .map(), .reduce(), .forEach() will skip these holes. [, , 1, , 2].forEach((item) => console.log(item)) // 1 // 2 ...

If I create a sparse array (which has some holes in it), the array functions, like .map(), .reduce(), .forEach() will skip these holes. [, , 1, , 2].forEach((item) => console.log(item)) // 1 // 2 ...

  1. javascript loop function every 5 seconds
  2. javascript loop function until condition is true
  3. javascript loop function every second
  4. javascript loop function
  5. javascript loop function call
  6. javascript loop function parameters
  7. javascript loop function delay
  8. javascript loop function settimeout
  9. javascript loop function time
  10. javascript function loop through array
  11. javascript function loop array
  12. javascript function loop through
  13. javascript timer loop function
  14. javascript loop through function arguments
  15. javascript loop async function
  16. javascript loop callback function
  17. javascript infinite loop function
  18. javascript loop inside function
  19. javascript for loop function closure
  20. javascript loop through function parameters

How do I display php code in handlebar template ? example - <script type="text/x-handlebars"> <span class="label label-default"> <?php echo "I am inside handlebar template" ?&...

How do I display php code in handlebar template ? example - <script type="text/x-handlebars"> <span class="label label-default"> <?php echo "I am inside handlebar template" ?&...

I've recently been spending some time playing with javascript event listeners in an attempt to learn a little more. However I've kinda hit a road block. I appear to be getting a syntax error, ...

I've recently been spending some time playing with javascript event listeners in an attempt to learn a little more. However I've kinda hit a road block. I appear to be getting a syntax error, ...

  1. bind onchange event jquery
  2. bind onchange event javascript
  3. bind onchange event react
  4. bind onchange event dropdown jquery
  5. js bind change event
  6. bind onchange event using javascript
  7. jquery bind onchange event to select
  8. jquery bind onchange event textbox
  9. bind to onchange event
  10. onchange event binding angular
  11. event binding onchange