Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function in text field

Status
Not open for further replies.

flnMichael

Programmer
Nov 13, 2003
96
US
Hey,
I want to have a value inserted into a text field on the page load from a javascript function. I am unfamiliar with the syntax to do that, if at all possible. Here's what I have

Code:
<input type=text size=10 disabled name=elapseddate value='AddDate(this.form);'>

I just want the function to happen on the page load, but the number of text fields is dynamic, so I can't do it in the body onload area. Can someone help me out with the syntax?

Thanks
Mike
 
Where is the data coming from and how will you know which field the data is destined for if the fields are dynamically generated?

If you pull the info from a database using ASP and you assign the data to an ASP variable named ElapseDate then you can have it automatically inserted using this:

Code:
<input type=text size=10 disabled name=elapseddate value='[COLOR=green]<%=ElapseDate%>[/color]'>



At my age I still learn something new every day, but I forget two others.
 
to do this in javascript, you should probably call the onload event (since you want the value to populate on page load):

Code:
function AddDate(elem) {
    elem.value = new Date();
}

window.onload = function() {
    AddDate(document.forms['formName'].elements['elapseddate']);
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Here's my process:

1. Load page with SQL Query to get past time stamps. This is where the dynamic text fields will come into play.

Code:
dbconn = "dsn=MyDatabase;uid=webqueries;pwd="
Set objConnection = Server.CreateObject("ADODB.Connection")
  objConnection.Open dbconn

SQLQuery = "select * from " & mytable
Set objRS = objConnection.Execute(SQLQuery)

2. These values are placed into a hidden variable 'hiddenDate"&index&"' through a while loop

Code:
i = 0
while not objRS.EOF
  Response.Write("<tr><td align=center>")
  Response.Write(objRS.Fields("Starttime"))
  Response.Write("</td><td align=center>")
  Response.Write("<input type=text size=10 disabled name=elapseddate"&i&" value='AddDate2(this.form,"&i&");'></td>")
  Response.Write("<td align=center>")
  Response.Write("<input type=hidden name=hiddenDate"&i&" value='")
  Response.Write(objRS.Fields("Starttime"))
  Response.Write("'>")
  Response.Write("</td></tr>")
  i = i + 1
objRS.MoveNext
wend

3. The AddDate function gets the current time stamp and the time stamp from the database and performs a difference to return the elapsed time in hours.

Code:
function AddDate(form,index)
{
  var now = new Date();

  var hour        = now.getHours();
  var minute      = now.getMinutes();
  var second      = now.getSeconds();
  var monthnumber = now.getMonth();
  var monthday    = now.getDate();
  var year        = now.getYear();

  var txtmonthnumber, txtmonthday, txthour, txtminute, txtsecond;
  var oldyear, oldmonth, oldday, oldhour, oldmin, oldsec;
  var sec_dif, hour_dif, min_dif, day_dif, month_dif, year_dif;
  var dateNow, dateThen;
    
  monthnumber = monthnumber + 1;

  if (monthnumber < 10)
   {
     monthnumber = "0" + monthnumber;
   }

  if (monthday < 10)
   {
     monthday = "0" + monthday;
   }

  if (second < 10)
   {
     second = "0" + second;
   }

  if (minute < 10)
   {
     minute = "0" + minute;
   }

  if (hour < 10)
   {
     hour = "0" + hour;
   }

  oldyear = form["hiddenDate"+index].value.charAt(0)+""+form["hiddenDate"+index].value.charAt(1)+""+form["hiddenDate"+index].value.charAt(2)+""+form["hiddenDate"+index].value.charAt(3);
  oldmonth = form["hiddenDate"+index].value.charAt(4)+""+form["hiddenDate"+index].value.charAt(5);
  oldday = form["hiddenDate"+index].value.charAt(6)+""+form["hiddenDate"+index].value.charAt(7);
  oldhour = form["hiddenDate"+index].value.charAt(9)+""+form["hiddenDate"+index].value.charAt(10);
  oldmin = form["hiddenDate"+index].value.charAt(11)+""+form["hiddenDate"+index].value.charAt(12);
  oldsec = form["hiddenDate"+index].value.charAt(13)+""+form["hiddenDate"+index].value.charAt(14);

  olddate = oldmonth+"/"+oldday+"/"+oldyear;
  oldtime = oldhour+":"+oldmin+":"+oldsec;
  newdate = monthnumber+"/"+monthday+"/"+year;
  newtime = hour+":"+minute+":"+second;

  dateDiff(olddate, newdate, oldtime, newtime, form, index);

dateDiff function
Code:
function dateDiff(olddate, newdate, oldtime, newtime, form, index)
{
  var totalhour;
  date1 = new Date();
  date2 = new Date();
  diff  = new Date();

  date1temp = new Date(olddate + " " + oldtime);
  date1.setTime(date1temp.getTime());

  date2temp = new Date(newdate + " " + newtime);
  date2.setTime(date2temp.getTime());

  // sets difference date to difference of first date and second date

  diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

  timediff = diff.getTime();

  weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
  timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

  days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
  timediff -= days * (1000 * 60 * 60 * 24);

  hours = Math.floor(timediff / (1000 * 60 * 60)); 
  timediff -= hours * (1000 * 60 * 60);

  mins = Math.floor(timediff / (1000 * 60)); 
  timediff -= mins * (1000 * 60);

  secs = Math.floor(timediff / 1000); 
  timediff -= secs * 1000;

  datedifference = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";

  totalsec = (weeks * 604800) + (days * 86400) + (hours * 3600) + (mins * 60) + secs;
  totalhour = totalsec / 3600;

  document.form3["elapseddate"+index].value = totalhour;
  return false; // form should never submit, returns false
}
I have been able to do it with a button onclick, but I need it to be loaded in when they come to the page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top