Dependent and form open
Dependent and form open
menashe
Posts: 196Questions: 43Answers: 2
I am trying to use field.dependent
to hide or show a <div>
. Butdependent
is apparently called before 'editor.open
such that elements that I place on the Editor Form (so that I can customize the layout) are not yet defined.
What event or method would work?
This question has an accepted answers - jump to answer
Answers
It sounds like you'd be better off using an event here, something like
open
orpreCreate
. This example may help - it's changing the size of the lightbox when the form is opened,Colin
dependent()
is used to change the form based on a selected value. That can happen as the form is being readied for display (i.e. an edit or create action), so it is correct before the end user sees it.Perhaps you could clarify a little more about what you want to do. As Colin indicates,
open
might be what you want, but it really depends on what you want to do.Allan
Allan/Colin,
Good morning! Yes, I have tried
open
and it works perfectly--when the Editor Form opens.I want to modify (show/hide) a field on the form every time the value of another field changes. I tried
dependent
, but it appears that runs before the form is "ready" such that my HTML elements to not yet exists.(This particular template looks as follows, with a number of HTML elements for which I control the layout:)
That should not be the case. The Editor DOM is constructed when the fields are added (either with
fields
oradd()
).What will happen is that the fields are inserted into the template structure when an editing action is triggered. If you are showing / hiding your own elements, rather than using
show()
/hide()
then that is when you might run into issues. Is that what you are doing? Perhaps you can show the code you are using so I have a better idea of what is going on?Allan
Sounds very reasonable.
I sent you the template this morning. The code to show/hide is:
As I mentioned, used in the
open
all three line work perfectly.Placed in a
dependent()
(or anything else that I've tried) the first two (show/hide) also work, but thestyle.display = 'none';
fails ("attempt to read null == reading 'style')."I am trying to hide the entire "Sale Price" row, below. As mentioned, it works in
open
.So how can I do that?
Thank you!!
Right - I'm with you now. Thank you for the code, that explains a lot.
Will select an element that is in the document (and that line assumes it will find it). However, since the Editor modal isn't inserted into the document until it is needed, that selector will find nothing, and since it assumes it will find an element, it throws the error you are seeing.
To access the element you need to use the
template()
method for your use case - e.g.:One point - the use of a
.
in an element's ID is valid per the HTML5 spec, but it makes selecting the element a lot more difficult since the.
means that it is a class name selector.I'd encourage you to avoid the use of dots / periods in an element's id.
Allan
Thank you!
template[0]
-- the "0" as in the first template?No matter how much I've learned (from you) about DataTables, jQuery, etc., there is always more to learn!
Thank you for the pointer regarding element IDs. I was doing it to remain consistent with the names that (I thought) are generated by DataTables when I look in Dev Tools!
I put
using the
dependent()
method, and I got:Sorry - my mistake. I totally thought
getElementById
was on all nodes. Apparently not!Try:
Note the need to escape the
.
. I'd really suggest you change the id to not include a period.Allan
PERFECT!!