# How to use addClass in oneditprepare

**URL:** <https://discourse.nodered.org/t/how-to-use-addclass-in-oneditprepare/50136>\
**Category:** Developing Nodes\
**Created:** [24 August 2021 22:20 UTC](https://discourse.nodered.org/t/how-to-use-addclass-in-oneditprepare/50136 "2021-08-24T22:20:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [24 August 2021 22:20 UTC](https://discourse.nodered.org/t/how-to-use-addclass-in-oneditprepare/50136/1 "2021-08-24T22:20:30Z")

</div>

Hi folks,

I need to add a red border around an input field, when some conditions are met:

```auto
if (...) {
   $('#node-input-myfield').addClass("input-error");
}

```

This works fine when this code snippet is called e.g. in an onchange event handler.  
However the class is not available on myfield, when I call this code snippet in `oneditprepare`.

I 'assume' that is because myfield is not yet visible at that moment?  
Does anybody know how I can solve this?

Thanks!!!

Bart

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 August 2021 22:47 UTC](https://discourse.nodered.org/t/how-to-use-addclass-in-oneditprepare/50136/2 "2021-08-24T22:47:09Z")

</div>

Oneditprepare should be able to add class to any element.

I assume `myfield` is one of the node's properties listed in its defaults object? If so, have you also set a validator function and/or set the required flag?

One possible explanation is that the editor will be validating your node after oneditprepare and if there's no error, it'll clear the input-error class.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [25 August 2021 06:13 UTC](https://discourse.nodered.org/t/how-to-use-addclass-in-oneditprepare/50136/3 "2021-08-25T06:13:55Z")

</div>

Hey Nick,  
Indeed I had a validator that was not functioning correctly yet.  
After fixing that, I could remove my addClass call in the oneditprepare (since the validator adds the class now). That was very stupid logic in my code...  
Thanks again!!  
Bart

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 September 2021 06:14 UTC](https://discourse.nodered.org/t/how-to-use-addclass-in-oneditprepare/50136/4 "2021-09-08T06:14:48Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
