I am trying to add some text to an html element by its ID randomly changing between a few different cases for different visitors.
For some reason, my text is not being added to the element.
Anyone see whats wrong with my code?
Here is a link to a JSBin thing i set up.
http://jsbin.com/biresocera/1/edit
<script>
var linkAnchor = $('#link-anchor');
var randShareLink = Math.floor(Math.random() * 5) + 1;
$(document).ready(function(){
if (randShareLink === 1){
$(linkAnchor).append('case 1');
} else if (randShareLink === 2){
$(linkAnchor).append('case 2');
} else {
$(linkAnchor).append('case 3');
}
});
</script>
<body>
<div id="link-anchor" style="background-color:red; height:40px; color:white; width:50px;"></div>
</body>
The code is fine. However, your jsbin is not set up correctly - you need to add the jquery library to make it work.
Also, use linkAnchor.append, as it is already a jquery object.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments