0

I am trying to shuffle some elements around but the onclick tag gets lost. How can I get the element including the tags?

var first = document.getElementById("demo1").innerHTML; // I want to keep the onclick tag for this one. 

Here is the rest of the code if you need it

HTML file:

<p id="demo"> Start </p> <p id="demo1" onclick="alert('hello')"> First </p> <p id="demo2"> Second </p> <p id="demo3"> Third </p> <script type="text/javascript" src="temp.js"> </script> <script> var first = document.getElementById("demo1").innerHTML; var second = document.getElementById("demo2").innerHTML; var third = document.getElementById("demo3").innerHTML; shuffleOptions(first, second, third); </script> 

temp.js file:

function shuffleOptions(first, second, third) { var options = [first, second, third]; options.sort(function(a, b){return 0.5 - Math.random(); }); document.getElementById("demo").innerHTML = options; } 
3
  • 1
    Instead of 'innerHTML' property, you can user outerHtml. Try this... Commented Feb 2, 2017 at 15:52
  • @NedimHozić It works. Thanks Commented Feb 2, 2017 at 15:53
  • Just wanna let you know that those are called "attributes." In HTML it's a good idea to say attribute instead of tag to prevent confusion. Tags usually refers to the element itself. Commented Jun 25, 2018 at 17:40

1 Answer 1

1

Try this

function shuffleOptions(first, second, third) { var options = [first, second, third].sort(function() { return .5 - Math.random(); }); document.getElementById("demo").innerHTML = options; } 
Sign up to request clarification or add additional context in comments.

Comments