I need to grab the html code between a span tag and assign it to a variable when the page first loads. Then I need to access that variable from within another function. No matter what I’ve tried I keep receiving the error “code is not defined”. I know that the code is being assigned to the variable because I’ve used the alert() command to verify it. So how can I make that variable accessible from another function?
[code]
<html>
<head>
<script>
onload = function() {
var code = document.getElementById(‘copy’).innerHTML;
alert(code);
}
function other() {
alert(code);
}
</script>
</head>
<body>
<span id=”copy”>
This is what I want to copy.
<br>
This is what I want to copy.
</span>
<br><br>
<h1 onclick=”other()”>Test the function</h1>
</body>
</html>