Jump to content

Timepicker JS - Only update if not null


Recommended Posts

I have a datapage with 2 date fields and I am using the timepicker to set the Time for them. The script I have is working, however I'd like to make some minor changes and it seems to be difficult for me.

document.getElementsByName('Mod0EditRecord')[0].onmouseover = function(){ 
var date = document.getElementById('EditRecordTech_Schedule_Table_2_Date').value;

if(date == "") { date = "1/1/2011"; }
var ampm = document.getElementById('timepicker1').value;
var ampm2 = document.getElementById('timepicker2').value;
  
document.getElementById('EditRecordTech_Schedule_Table_2_Time_From').value = date + " "  + ampm;
document.getElementById('EditRecordTech_Schedule_Table_2_Time_To').value = date + " "  + ampm2;

};

Right now, this is working if you give both Timepickers a time. However, if you only update one of the timepickers, the other defaults to '12:00:00'. I would like it to not update that particular date field if the timepicker is blank/null.

 

Any help is appreciated.

Link to comment
Share on other sites

I have tried inserting an IF statement -  if (ampm !== null) - but it is updating it to the default '12:00:00' regardless. 

 

**Here is the script if anyone is looking for the answer to this problem.

document.getElementsByName('Mod0EditRecord')[0].onmouseover = function(){ 
var date = document.getElementById('EditRecordTech_Schedule_Table_2_Date').value;

if(date == "") { date = "1/1/2011"; }
var ampm = document.getElementById('timepicker1').value;
var ampm2 = document.getElementById('timepicker2').value;
  
if (ampm !== null && ampm !== ''){
document.getElementById('EditRecordTech_Schedule_Table_2_Time_From').value = date + " "  + ampm;
}
  
if (ampm2 !== null && ampm2 !== ''){
document.getElementById('EditRecordTech_Schedule_Table_2_Time_To').value = date + " "  + ampm2;
}
};

 

Edited by kpcollier
Added solution
Link to comment
Share on other sites

20 hours ago, kpcollier said:

I have tried inserting an IF statement -  if (ampm !== null) - but it is updating it to the default '12:00:00' regardless. 

 

Here is the script if anyone is looking for the answer to this problem.


document.getElementsByName('Mod0EditRecord')[0].onmouseover = function(){ 
var date = document.getElementById('EditRecordTech_Schedule_Table_2_Date').value;

if(date == "") { date = "1/1/2011"; }
var ampm = document.getElementById('timepicker1').value;
var ampm2 = document.getElementById('timepicker2').value;
  
if (ampm !== null && ampm !== ''){
document.getElementById('EditRecordTech_Schedule_Table_2_Time_From').value = date + " "  + ampm;
}
  
if (ampm2 !== null && ampm2 !== ''){
document.getElementById('EditRecordTech_Schedule_Table_2_Time_To').value = date + " "  + ampm2;
}
};

 

What happens? Do you have a sample DataPage? nothing happens when my 2 fields are null (2and 3 for timepicker 1 and 2)

 image.png.f9688104029bbbe4475c9b59981ecc89.png

If I put a value, it appends,

image.png.d0d23dece128a4a2aec0ca83b2d08ef9.png

 

Maybe you have some stray script or triggered action?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...