Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Use JS to populate form field with current date/time

  • 24-12-2009 03:55PM
    #1
    Registered Users, Registered Users 2 Posts: 319 ✭✭


    I'd like to use Javascript to populate a given form field when it's mouseover'd. JS isn't really my forte, so after some tinkering, I've got this function.
    function currdatetime(){
    	var now = new Date();
    	var dateit = now.getDate();
    	var monthit = now.getMonth();
    	var yearit = now.getFullYear();
    	var hourit = now.getHours();
    	if(hourit < 10)
    		hourit = "0" + hourit;
    	var minuteit = now.getMinutes();
    	if(minuteit < 10)
    		minuteit = "0" + minuteit;
    	var secondit = now.getSeconds();
    
    	currentdatetime = dateit + "/" + monthit + "/" + yearit + " " + hourit + ":" + minuteit + ":" + secondit;
    	return currentdatetime;
    	}
    
    which works fine if I just want:
    var datetimeit = currdatetime();
    document.write(datetimeit);
    
    I think I may be limited in what I can do in the onMouseOver section, so I guess
    onMouseOver='javascript:document.formname.itemname.value=currdatetime();'
    
    is a nono.

    Can anyone fix where I've gone wrong here?


Comments

  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    change the onmouseover part to
    onmouseover="currdatetime();"
    

    then in your function replace the return statement with
    document.getElementById("textboxID").value=currentdatetime;
    
    where the textboxID is the actual ID of your textbox.


  • Registered Users, Registered Users 2 Posts: 319 ✭✭Jaeger


    Had started thinking along those lines, with the exception of the getElementByID bit!

    If the inputbox doesn't have an ID, just a name, will that still work tho? Doesn't so far..


  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    var textbox =document.getElementsByName("name");
    textbox[0].value=currdatetime;
    
    document.getElementsByName() returns an array of elements matching the name in quotes.
    More info here:
    http://www.w3schools.com/jsref/met_doc_getelementsbyname.asp


  • Registered Users, Registered Users 2 Posts: 319 ✭✭Jaeger


    Thanks for the help friend. Unfortunately, not working. Am trying to get it to work with existing joomla plugin, and am coming to the conclusion it's joomla interfering with the call to the function, as that's the bit that doesn't seem to be working at all.


  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭smcelhinney


    Joomla shouldnt interfere unless it overwrite/re-declares variables. This is pure javascript, so as long as its correct in the source code, it should be fine.

    you can debug using Firebug for Firefox if you think there is a javascript error, but your/duffman's code looks sound.


  • Advertisement
Advertisement