Hi i have created a span element with a text in a containing 0s and 1s. Is there a way which i can randomly select any character in that span element. I know how to do this by putting each character inside a separate span element but i’m creating this for a background and there will be over 2500 0s and 1s.
Html
<span class="text" id= "text">0001111011110110101100001</span>
JavaScript
var text = document.getElementById('text');
var x = Math.floor(Math.random() * text.length);
>Solution :
This function will return a random character from your span content
let content = document.querySelector('#text').textContent;
function getRandomChar(str){
return str.charAt(Math.floor(Math.random() * str.length));
}
console.log(getRandomChar(content))
<!-- I added another content just for better demonstration -->
<span class="text" id="text">00ABCDEFGHIJK</span>