Inline Javascript not executed
I want to set the current time for an element with moments.
<span onload="$(this).text(moment().format('MMMM Do YYYY, h:mm:ss a'));"></span>
What's bad about it? Nothing is displayed.
+3
daniel
source
to share
2 answers
onload is not what you want (it doesn't even work), you need to execute this on document.ready
I think you are mixing jQuery and JavaScript, try using document.ready
in a script block.
First we will give your ID range
<span id="yourSpan"></span>
Then we can do:
<script type="text/javascript">
$(function () {
$("#yourSpan").text(moment().format('MMMM Do YYYY, h:mm:ss a'));
});
</script>
EDIT: As you said, you want it inline (although this is really bad practice and I'll have to wash my hands after writing it), add your ID to your range as above, but do this:
<body onload="$('#yourSpan').text(moment().format('MMMM Do YYYY, h:mm:ss a'));">
+8
mattytommo
source
to share
onload is not supported in span tag, some tags that support onload:
<body>, <frame>, <frameset>, <iframe>, <img>,
<input type="image">, <link>, <script>, <style>
+6
mpm
source
to share