I have the following code:
echo "<div class='hp_container'><div id='hp' val=".$warrior_row[8]."></div></div>";
In this case, val has the value 8.
I want to access this value with JavaScript. I have tried the following method, but it keeps saying that txt is null.
<script type='text/javascript'>
var txt = document.getElementById("hp");
var f = txt.getAttribute('val');
alert(f);
</script>
How can I resolve this?
Edit
I solved the problem by placing the code inside window.onload:
window.onload = function() {
var txt = document.getElementById("hp");
var f = txt.getAttribute('val');
alert(f);
};