Here is my Javascript. I'm trying to add a character counter to a text field. I created a new_charactersremaining field as a Whole Number, Simple field type to count the number of characters.
As you can see, onload3 is supposed to call the keyPress function, but I'm getting the following error.
TypeError: Cannot set property 'onkeyup' of null
function keyPress() {
var charRemaining = Xrm.Page.getAttribute('new_charactersremaining');
var message = document.getElementById('description');
var messageVal = message.value;
var maxLength = 200;
Xrm.Page.ui.setFormNotification("Message: ", messageVal);
if (messageVal != null) {
var msgLength = messageVal.length;
var remaining = maxLength - msgLength;
charRemaining.setValue(remaining);
}
else {
charRemaining.setValue(maxLength);
}
}
function onload3() {
var text = Xrm.Page.getAttribute('description').getValue()
if (text == null){
Xrm.Page.getAttribute('description').setValue("Text goes here.");
}
document.getElementById('description').onkeyup = keyPress;
document.getElementById('description').onkeydown = keyPress;
<!-- Xrm.Page.getAttribute('description').addOnKeyPress([keyPress]); -->
}
I'm trying to follow this guys code.
https://www.magnetismsolutions.com/blog/paulnieuwelaar/2011/05/24/Dynamics_CRM_2011_Calling_OnKeyDown_With_Javascript.aspx
Any help is much appreciated. Thanks!