Forums

How can i add a control to clear/empty a prefilled date field?

NickOg 13 Sep, 2016
Hi

I was sure I had seen this somewhere before but can't find it.
I have a set date picker fields pre-filled with dates (for classes). I need a control for each date so that I can clear a specific date. How would I do that?

Regards

Nick
GreyHead 14 Sep, 2016
Hi Nick,

I'm not sure that I understand the question :-( Could you add buttons and use JavaScript snippets to clear the input value when they are clicked?

Bob
NickOg 14 Sep, 2016
Hi Bob,
Made a bit of a pig's ear about this one. Yes - a button and JavaScript. I was worrying that I had seen some feature of datepicker that dealt with setting a null date but have now found the code that I was looking for. You helped me with that quite a while ago.

sorry to have wasted your time .:|

Nick
NickOg 15 Sep, 2016
Hi Bob

I found what I was looking for - some jQuery that you gave me a while ago. But this works
<input id='njjoidimgweek2'  
title='Click: clear date.' 
type='image'  class="njjoclsErase" src="components/com_u3aV5/common/img/erase.png"
onclick=clearFields('njjoidweek2');
/>

function clearFields(inputToClear) {
  fieldToClear = document.getElementById(inputToClear);
  $(fieldToClear).value = '';
  return true;
}


The only trouble is that this causes the form to be submitted when i clear a field. How can I stop that submit?

Regards
Nick
GreyHead 15 Sep, 2016
Hi Nick,

Hmmm from a quick Google, please try replacing return true; with return false; I think that should stop the submit behaviour.

Bob
NickOg 15 Sep, 2016
No difference Bob - still submits the form.
Nick
NickOg 15 Sep, 2016
Answer
Eureka! Your solution plus a missing piece.

<input id='njjoidimgweek2'  
title='Click: clear date.' 
type='image'  class="njjoclsErase" src="components/com_u3aV5/common/img/erase.png"
onclick="return clearFields('njjoidweek2')"
/>


apart from a typo (a missing " that had no effect) I had to add that return to the onclick event
onclick="return clearFields('njjoidweek2')"



Nick
This topic is locked and no more replies can be posted.