最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to display Javascript variable in specific HTML div? - Stack Overflow

programmeradmin1浏览0评论

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=".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?

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./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?

Share Improve this question edited Oct 13, 2014 at 12:44 user3797115 asked Oct 13, 2014 at 10:47 user3797115user3797115 411 silver badge4 bronze badges 7
  • google your query first, before asking. – Ravi 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 – user3797115 Commented Oct 13, 2014 at 10:50
  • Can you show us how you tried to do it? – STT Commented Oct 13, 2014 at 10:50
  • Check your browser console for errors, and what functions have you used to manipulate DOM? – Ravi Commented Oct 13, 2014 at 10:51
  • I get this error Uncaught TypeError: Cannot set property 'innerHTML' of null – user3797115 Commented Oct 13, 2014 at 10:59
 |  Show 2 more ments

6 Answers 6

Reset to default 3

You may try:

document.getElementById("your_div_id").innerHTML = myAge;

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;

HTML:

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

JS:

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

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;

If your variable is your_variable Then you can use

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

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 bees:

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);
发布评论

评论列表(0)

  1. 暂无评论