Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a javascript from html using an id

I have the following footer html code

<footer class="footer">
        <div class="container">
            <p class="text-muted"> <a href="about.html">About Us</a> | <a href="contactus.html">Contact Us</a></p>
            <p class="text-muted">&copy; Copyright of abc
                <span id="yearfooter"> </span>
            </p>
        </div>
</footer>
<script src="scripts/abc.js"></script>

and the following for my jscript

$(function() {
$('#yearfooter').footer({
    document.write(new Date().getFullYear());
});

});

I'm fairly new to javascript. I'm trying to get this script to dynamically get the current year and display that in the footer. But with this, I cant seem to get the year displayed at all. I'm not sure what I'm doing wrong here.

like image 581
Michelle Ashwini Avatar asked Jul 20 '26 07:07

Michelle Ashwini


1 Answers

Using document.write will overwrite the webpage, so you want something like this:

$(function() {
    $('#yearfooter').text(new Date().getFullYear());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<footer class="footer">
        <div class="container">
            <p class="text-muted"> <a href="about.html">About Us</a> | <a href="contactus.html">Contact Us</a></p>
            <p class="text-muted">&copy; Copyright of abc
                <span id="yearfooter"> </span>
            </p>
        </div>
</footer>
like image 102
AMACB Avatar answered Jul 21 '26 23:07

AMACB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!