Hello There,
I'm trying to use the GetElementByID() function, but it doesn't work.
Can someone help me?
Note: The following printscreens show tests with the id 'b6-IPTNAME', but I also tested with:
'$b6-IPTNAME'
'"b6-IPTNAME"'
'#b6-IPTNAME'
'#"b6-IPTNAME"'
'"#b6-IPTNAME"'
My Javascript code:
My error message:
My Inspect (shows the input id):
I solve my problem and i'm posting this aswer with the details to help another users with the same problem.
But I need to mention first that it would be impossible to solve without the help of @Daniël Kuhlmann and @Marco Mateo.
My problem starts in that post.
My goal was to validate the data on the rest api side and simplify the app side to work without dependencies. That is, I would like to make validation changes in the rest api side without need to make changes on the app side.
IN THE REST API SIDE:
To solve this goal i created a rest api that validate the fields.
For each invalid field, the rest api appends the field ina list containing FieldName and ErrorMessage.
IN THE APP SIDE:
I have a save button that calls the rest api and fills the ScreenElements in a local variable called InvalidFields. I need to do this because I need to access it in a client action called OnRender in the following.
In the end of the save client action, the app runs the OnRender client action.
OnRender client action checks if the InvalidFields list has data.
If has, it calls another client action called FillInvalidFieldsMessages (This is separate to be reused on all forms of the app).
FillInvalidFieldsMessages client action loop the invalid fields, converts them to json, and runs a javascript code to fill the error messages in the form.
Done!
THE JAVASCRIPT CODE TO FILL THE ERROR MESSAGES:
block = document.getElementById($parameters.FormId);if (block) { blockId = block.parentNode.id; blockId = blockId.replace('$', ''); var screenElementJson = JSON.parse($parameters.ScreenElement); var elementId = blockId + "-" + screenElementJson["FieldName"]; var element = document.getElementById(elementId); if (element) { element.classList.add("not-valid"); var validationMessageSpan = document.createElement('span'); validationMessageSpan.innerHTML = screenElementJson['ErrorMessage']; validationMessageSpan.classList.add("validation-message"); validationMessageSpan.classList.add("custom-invalid"); element.parentNode.appendChild(validationMessageSpan); }}
THE JAVASCRIPT CODE TO CLEAN THE ERROR MESSAGES (I didn't mention this in the answer because I'm still thinking about where to put this):
var elements = Array.from(document.getElementsByClassName("custom-form-input"));elements.forEach( element => { element.classList.remove("not-valid");});
var elements = Array.from(document.getElementsByClassName("validation-message custom-invalid"));elements.forEach( element => { element.parentNode.removeChild(element);});
FINAL NOTES:
If anyone knows a smarter and safer way to do this, I'd be happy to know.
Hi,
You should not try to access the Element IDs, because they are automatically generated by OutSystems. They will change as you change your screen design. Use the element name or use specific classes if you really need to access those DOM elements from JavaScript.
What you can do is obtain this id at runtime (each named widget will have an Id runtime attribute you can only read) and inject it in your JavaScript node.
Regards,
Daniel
Hello Daniel,
I think that I understand what do you want to say.
I have a save button with a save client action and the system runs that process after user click.
I'm trying to get the elements by Id with FillInvalidInputMessages client action inside the save client action.
But that doesn't work because the Id doesn't exist before the save client action process comes to the end.
To fix this I need to run the FillInvalidInputMessages client action On Render. Right?
I will try that tomorrow and come back to let you know if it worked.
Hi @Thales Ferraz
In this post https://www.outsystems.com/forums/discussion/36146/how-to-use-getelementbyid-javascript-in-outsystems/
You will find your solutions.
Hello Bruno,
I saw this post before asking.
The attempts below were taken from it:
Hi Thales,
I suggest to make an input parameter in your JS node and assign it with the [elementName].Id
Sample:
Note:
You have to "name" the element before you can access its Id.
Hello Marco,
What you said was really smart.
It doesn't solve what do I ask with this post, but it solves several problems I have.
It is the same what I also wrote (less verbose and without screenshots) in my first reply
Working!
You are great.
I will make a post explaining the details and marking it as a solution.