Jump to content

Javascript Code Problems


natasha23

Recommended Posts

hay guys i ave come up with this code to display the date in a textbox but for some reason its showing on the web page but not within the text box and im not sure why, here is my code:var rightNow = new Date() var m = rightNow.getMonth() + 1 var d = rightNow.getDate() var y = rightNow.getFullYear() var todayDate = (d + "/" + m + "/" + y) document.write(todayDate); document.getElementById("task2").todaysDate.value = todayDate;if you could help me out that would be great.thanks in advance

Link to comment
Share on other sites

There are probably several problems here.1. You don't explain where this code is, so I have to make some guesses.2. If the code occurs in a function that is triggered by an onload event or a click or something, then your document.write() statement is erasing your page contents and replacing them with the date, leaving you with no script and no text element.3. If the code does not occur in a function, then I guess it runs as the page is loaded. If the code is ABOVE your text element (because your script is in the <head> element), then the code won't work, because the text element is not yet part of the page when the code runs. For efficiency, browsers run Javascript at the same instant they read it. They don't wait, unless you tell them to wait by using a window.onload handler. An alternative is to place the script BELOW the text element.4. In any case, the line where you try to pass data to your text element is incorrect:document.getElementById("task2").todaysDate.value = todayDate;The "todaysDate" part doesn't mean anything. Assuming your document actually has a text element with id="task2", then just write this:document.getElementById("task2").value = todayDate;

Link to comment
Share on other sites

There are probably several problems here.1. You don't explain where this code is, so I have to make some guesses.2. If the code occurs in a function that is triggered by an onload event or a click or something, then your document.write() statement is erasing your page contents and replacing them with the date, leaving you with no script and no text element.3. If the code does not occur in a function, then I guess it runs as the page is loaded. If the code is ABOVE your text element (because your script is in the <head> element), then the code won't work, because the text element is not yet part of the page when the code runs. For efficiency, browsers run Javascript at the same instant they read it. They don't wait, unless you tell them to wait by using a window.onload handler. An alternative is to place the script BELOW the text element.4. In any case, the line where you try to pass data to your text element is incorrect:document.getElementById("task2").todaysDate.value = todayDate;The "todaysDate" part doesn't mean anything. Assuming your document actually has a text element with id="task2", then just write this:document.getElementById("task2").value = todayDate;
thank you for your help i just realised by putting the javascript at the bottom i was able to write the date into the text box .....silly mistake
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...