natasha23 Posted April 3, 2009 Share Posted April 3, 2009 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 More sharing options...
jeffman Posted April 3, 2009 Share Posted April 3, 2009 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 More sharing options...
natasha23 Posted April 5, 2009 Author Share Posted April 5, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.