1

I'm very new to using Javascript and I want to display this variable on my homepage for my portfolio

<html lang="en">
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>

    var curDate  = new Date();
    var curYear  = curDate.getUTCFullYear(),
        curMonth = curDate.getUTCMonth(),
        curDay   = curDate.getUTCDate();

    var myYear   = 1996,
        myMonth  = 10,
        myDay    = 22;

    var myAge = curYear % myYear;
    if (curMonth < myMonth && curDay < myDay || curMonth < myMonth && curDay === myDay || curMonth == myMonth && curDay < myDay) {
      myAge  -= 1; 
    }

    var myAgeDiv = document.getElementById('my-age');
    myAgeDiv.textContent = myAge;

    $('#my-age').text(myAge);

    </script>
    <title>My eportfolio!</title>
</head>
<body>
    <div id="my-age"></div>
</body>
</html>

Edit: I've created a new html file (the same as above) with no other content external .js files and it still doesn't work. Does this mean the age displaying code is wrong?

7
  • google your query first, before asking. Commented Oct 13, 2014 at 10:47
  • I have but literally none of the solutions work I don't know if there's an error in the code Commented Oct 13, 2014 at 10:50
  • Can you show us how you tried to do it? Commented Oct 13, 2014 at 10:50
  • Check your browser console for errors, and what functions have you used to manipulate DOM? Commented Oct 13, 2014 at 10:51
  • I get this error Uncaught TypeError: Cannot set property 'innerHTML' of null Commented Oct 13, 2014 at 10:59

6 Answers 6

3

You may try:

document.getElementById("your_div_id").innerHTML = myAge;
Sign up to request clarification or add additional context in comments.

Comments

2

if your div have an ID do this using jQuery:

$('#DivID').text(myAge);

have a class:

$('.DivClass').text(myAge);

or using only javascrtipt:

document.getElementById('DivID').innerText = myAge;

Comments

2

HTML:

<div id="test"></div>

JS:

var d = document.getElementById('test');
d.innerHTML = myAge;

Comments

2

Let's say you've got a <div> like the following one:

<div id="my-age"></div>

Then you can use the document.getElementById() to get the div in JavaScript and edit its content:

var myAgeDiv = document.getElementById('my-age');

myAgeDiv.textContent = myAge;

3 Comments

And this got downvoted because...? The answer is correct, why should anyone downvote it? Please explain and do not just "random downvote" answers.
This is awful I know all these answers are great and should work but they don't. btw I up-voted
@user3797115 then you are doing something wrong! Tell us more, edit your question and add additional details.
2

If your variable is your_variable Then you can use

document.getElementById('DivID').innerHTML = your_variable;

Comments

1

If you are using pure javascript, somehow get your element, for example:

var myElem = document.getElementById('my-element-id');

Then you can set it's innerHTML:

myElem.innerHTML = myAge.toString();


If you are using jQuery, things get simpler, getting your element becomes:

var myElem = $('#my-element-id');

And setting it's content:

myElem.html(myAge);

Or if you don't need to insert html and want to be safer:

myElem.text(myAge);

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.