+ Start a Discussion

javascript onclick for date field text link

Hi All:


I'm trying to capture the onclick event for the current date text link on a date inputfield. The issue I'm having is if I place the onclick event on the inputfield it applies to the generated text field in the HTML but not the link with the current date. Is there a way to get an onclick event on the generated date text link?





You can use jQuery or another library (it includes support for triggering functions when a JS function modifies a field), or you can use a "watchdog" sort of function (periodically poll to check for a value change, and act accordingly). You could theoretically hook into the JS function that the default calendar widget provides, but the standard warning applies here: modifying built-in functions can cause undesirable side effects or break on a future release or hotfix. There's no "great" solution to this problem because of the nature of the system library.


Thanks for the reply! I was trying to avoid using jQuery since I'm only using javascript on one page for one function. The second and third options you've suggested sound interesting.


The problem is that HREF doesn't have an ID associated with it and adding one to the apex:inputfield only applies to the textbox not the HREF link. How could I reference this link reliabliy without being able to assign it an ID?


I've come up with an ugly fix that seems to work, but its not pretty :\ . I'm open to any solutions on how to clean this up without introducing a jscript framework.


var alertval="test";
var wrapperid=document.getElementById("dateclass");
var myhreflist= wrapperid.getElementsByTagName("a");
myhreflist[0].setAttribute('onclick', 'alert(alertval)');

<span id="dateclass">
<apex:inputField value="{!contact.testdate__c}" id="date"/>