3

I want to set the current time to an Element with momentjs.

<span onload="$(this).text(moment().format('MMMM Do YYYY, h:mm:ss a'));"></span>

What is wrong with it? Nothing is shown.

2
  • 6
    load events are not triggered on span elements. Commented Jan 24, 2013 at 13:25
  • The best Answer is a combination of mattytommo and camus Commented Jan 24, 2013 at 13:29

2 Answers 2

7

onload is not what you need (it won't even work), you need to be executing this on document.ready

I think you're mixing jQuery and JavaScript, try using document.ready in a script block.

First we'll give your span an ID

<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've said you want it inline (although it's really bad practice and I'll have to wash my hands after writing it), still add your ID to your span as above, but do this:

<body onload="$('#yourSpan').text(moment().format('MMMM Do YYYY, h:mm:ss a'));">
Sign up to request clarification or add additional context in comments.

5 Comments

i want it completely inline
@zoidbergi Is it a limitation that it has to be inline? It's really bad practice to do that.
@zoidbergi Wise choice! :)
i hope your hands are clean again ;)
@zoidbergi Nothing a bit of methlyated spirit and bleach can't get rid of :D
6

onload is not supported in the span tag , some tags that support onload :

<body>, <frame>, <frameset>, <iframe>, <img>, 
<input type="image">, <link>, <script>, <style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.