user985952
user985952

Reputation: 97

Can't insert date into any HTML element

All I am trying to do is insert today's date into a TD

<script type="text/javascript">
    $(document).ready(function(){
    var myDate = new Date();
    $(myDate).appendTo("#calcShptable td:first");
    });
</script>

Just doesn't do anything.. no errors or anything.

Upvotes: 0

Views: 336

Answers (4)

Jim H.
Jim H.

Reputation: 5579

You have your value and your element selector reversed. Try

$('#calcShptable td:first').append(myDate.toString()); // Append text, not appendTo()

Upvotes: 1

Jasper
Jasper

Reputation: 76003

You've created a date object and what you want to do is create a date string like this:

var today  = new Date(),
    myDate = today.getDate() + '/' + (today.getMonth() + 1) + '/' + today.getFullYear();
$("#calcShptable td:first").append('<span>' + myDate + '</span>');

Here is a link to some documentation for the JavaScript Date Object (you can create a date in any format you like): http://www.w3schools.com/jsref/jsref_obj_date.asp

Upvotes: 0

Joseph Marikle
Joseph Marikle

Reputation: 78520

You can't just insert a string like it was a node. Make it into a node first:

$(document.createTextNode(myDate)).appendTo("#calcShptable td:first");

http://jsfiddle.net/n4xcG/

Upvotes: 1

Royi Namir
Royi Namir

Reputation: 148524

<script type="text/javascript">
    $(document).ready(function(){
    var myDate = new Date();
    ("#calcShptable td:first").append(myDate.toString());
    });
</script>

Upvotes: 2

Related Questions