Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Javascript character counter receiving TypeError: Cannot set property 'onkeyup' of null

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript character counter receiving TypeError: Cannot set property 'onkeyup' of null

    According to the first Reply on this discussion thread, .addOnKeyPress doesn't work with Multiple Lines of Text. If so, what can I do?

    community.dynamics.com/.../229260

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript character counter receiving TypeError: Cannot set property 'onkeyup' of null

    @Ben Thompson Thanks for the help! I'm now getting this error.

    TypeError: Xrm.Page.getControl(...).addOnKeyPress is not a function at onload3

    The description field is a Multiple Lines of Text Type.

    According to the documentation, text and number fields should work as shown here.

    msdn.microsoft.com/.../gg334266.aspx

  • Suggested answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: Javascript character counter receiving TypeError: Cannot set property 'onkeyup' of null

    I believe the structure and layout of Dynamics 365's forms have changed somewhat since 2011 days - so that code is definitely out of date.

    The  first document I can find that references the more recent way of handling keypress events is www.powerobjects.com/.../two-new-features-for-keypress-in-dynamics-crm-2016 which should point you in the correct direction going forward.

    I suspect the Javascript you now need is  Xrm.Page.ui.controls.getByName("description").addOnKeyPress=keyPress rather than document.getElementById("description")

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans