JavaScript Field - Get the values of the other form fields

44 views
Skip to first unread message

Daniel Borreguero Albitos

unread,
Jan 23, 2024, 5:56:27 AMJan 23
to Magnolia User Mailing List
Hi! 

I have a javascriptField inside a jcrMultiField and I want to access from each javascriptField to the value of the others, but I can't do it with JS because each field it's an independent html document, and the only way I've found it's accessing the jcr with a rest end point, but that only works after saving the dialog. 

Any ideas to do it in real time without having to save?

Thank you and best regards :)

Sebastian Geschke

unread,
Jan 23, 2024, 8:22:13 AMJan 23
to Magnolia User Mailing List, daniel-borre...@rentokil-initial.com
Hi Daniel!

You can listen to change events in your javascriptField using the following code: 
window.addEventListener(
"message",
function (event) {
if (event.data.action === "init") {
console.log("Init event"); // Store correlation id
console.log(event.data.correlationId);
console.log(event.data.state);
} else if (event.data.action === "change") {
console.log("Change event");
console.log(event.data.state);
}
},
false,
);

To propagate values for the fields use the following snippet (Note you need to store correlationId in init event)
parent.window.postMessage(
{
action: "changeValue",
correlationId,
value: input.value,
},
"*",
);

Let us know how it goes
Sebastian
Reply all
Reply to author
Forward
0 new messages