Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Use JS to populate form field with current date/time

  • 24-12-2009 2: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