1
I’m developing a PHP system that uses an extension for dynamic forms, where I can click a button to add fields (EX: A request can have multiple items, where the items are the fields added dynamically).
The extension I use (I don’t think it’s necessary to mention, since the problem isn’t with it) has in its configuration a parameter where I can specify the field limit that can be added dynamically, can be infinite or not.
When the extension is rendered, it generates a snippet of javascript code, where in this snippet, there is a variable that contains a JSON with the previously established parameters, among them the parameter "limit", which is the parameter I want to change at runtime.
So far so good... The problem is that such a variable that contains JSON, has a random suffix in the name, for example dynamicform_5ed2807a or dynamicform_7fg2802c.
The name of this variable I can find out, because there is a div with the fixed class dynamicform-wrapper containing the attribute data-dynamicform, and the value of this attribute is exactly the variable name.
Therefore, I tried to do as follows, without success:
var x = $(this).attr('data-dynamicform');
console.log(x.limit); // Note que limit é o nome da 'key' que eu quero pegar o valor
In the example above, it returns Undefined, and should return an integer number, which is the field limit.
EDIT
Following link from Jsfiddle: https://jsfiddle.net/wuoo6Lpm/
How can I solve this problem?
Thank you very much!
you can show how the HTML of that element is
data-dynamicform
? If that’s a JSON you need to dovar x = JSON.parse($(this).attr('data-dynamicform'));
– Sergio
Hello. Actually the html attribute looks like this:
data-dynamicform="dynamicform_5ed2807a"
. JSON is the variable I want to access, which has the same attribute name.– jflizandro
So what do you expect to return with
x.limit
?– Sergio
I hope to return the value of limit, which is in the javascript variable of the same name. See:
var dynamicform_5ed2807a = {"widgetContainer":"dynamicform_wrapper","widgetBody":".container-encaminhamentos","widgetItem":".item-encaminhamento","limit":4}
– jflizandro
Ah, I see. And this variable is created as? via PHP? can you change the way it is created?
– Sergio
It is created with random name, so I need to access it by the data-dynamicform attribute, because its value is the same variable name.
– jflizandro
But can you control this Javascript in PHP? you can change to be for example
dynamicforms.dynamicform_5ed2807a = {"widgetCont...
? Withoutvar
, but adding a new property to an object.– Sergio
No, because in case of updating the extension in PHP, I lose all the changes I made. And it is updated via dependency manager.
– jflizandro
ok... and this
var
is in the global scope at least?– Sergio
Yes. It looks like this: https://jsfiddle.net/wuoo6Lpm/
– jflizandro