648
Views
10
Comments
Solved
Get element Id from HTML
Question
Application Type
Reactive

Hi

For a project I am working on we are creating a guided tour based on some JavaScript. The JS works with the HTML elements so it knows what div/button etc to highlight. 

However, it seems that the actual Id of the HTML elements changed. For instance, I had specified one Id as #b7-WorldMap, but noticed that when I check now it is #b6-WorldMap in the HTML. This causes errors obviously. I have given the container the name WorldMap and was expecting the Id to always be the same. 

How would I get the ElementId by the element name?

Another JS with GetElementById() won't help me in this case... 


Max

2020-07-02 13-32-59
Max de Groot
Solution

Thanks for all the answers! 

I managed to resolve it by explicitly defining the name as attribute (kinda like Damian suggested). 


Then with this JS:


var elements = document.getElementsByName($parameters.ElementName);

var id = elements[0].getAttribute('id');

$parameters.ElementId_Out =  id;


I get back the required Id.


PS. I though it would be sufficient to specify the Name attribute already given by OutSystems, but this apperantly turn into the Id, making the name unavailable. 

2023-02-09 12-36-42
Damian Fonville
Solution

Hi Max,

 I don't think saving the ID is the best option because IDs can be dynamic. A better solution could be adding a custom data attribute to every element you want to save. This way, you have control over each ID, and they will only change when a developer modifies an ID. By doing this, you can create a static entity and use this ID to store it in the database and assign it to an element.



https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes 



2023-02-09 12-36-42
Damian Fonville

Hi Max,

If you want to get the Id of a Widget you could assign a name to it, Then when you want to execute Javascript you could pass the Id to the input parameter, see my screenshot. 

Description:
Id runtime property : data type Text (Read Only) Identifies the widget instance at runtime (HTML 'id' attribute). You can use it in JavaScript and Extended Properties. 

2023-07-04 08_18_37-Window.png
2025-10-18 11-13-53
Ramesh subramanian

Hi Max,

Please find the attached OML file.

HTML  = document.getElementById($parameters.ElementId).innerHTML;


Thanks,

Ramesh

DemoAppShowImgJS.oml
2020-07-02 13-32-59
Max de Groot

Hi Ramesh, 

Thanks but this is not what I am looking for. 
As mentioned I don't know the Id of the HTML element so GetElementById will not work.

I need to get the ElementId by the ElementName that is specified in OutSystems. The Id seems to be inconsistent.

2025-10-18 11-13-53
Ramesh subramanian
2020-07-02 13-32-59
Max de Groot

Hi Damian,

Maybe this was not clear enough, but I want to be able to get the Id by the name of the element in run time. 

My application should be able to let end users create their own guided tour based on the elements selected by them. This means that if they create the tour in June, it should still work (with the same Id's etc in October). I store the ElementNames that they have selected in an entity and match these with an actual tour. So above will not work since I don't know the elements they want to use.

2022-03-19 07-12-06
Alec Manabat

Hi @Max de Groot,

If you want to get the id of a container or a widget and use it in a javascript code or function, you should pass the id as input parameter in a javascript action, and inside that javascript, you should now have the id of the element you want to get. See images below for reference.


Hope you get the idea and this helps.

Regards,
Alec

2020-07-02 13-32-59
Max de Groot

Thanks, but not what I am looking for.

I don't know the Id and need some dynamic solution since end users will be able to create the guided tour and select the elements.

2023-02-09 12-36-42
Damian Fonville
Solution

Hi Max,

 I don't think saving the ID is the best option because IDs can be dynamic. A better solution could be adding a custom data attribute to every element you want to save. This way, you have control over each ID, and they will only change when a developer modifies an ID. By doing this, you can create a static entity and use this ID to store it in the database and assign it to an element.



https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes 



2024-01-04 15-15-51
Abed Al Banna

Hi @Max de Groot 

OutSystems dynamically creates IDs based on the names of the containers, it is not recommended to use the exact ID hard-coded (example #b5-WorldMap) as these ID prefixes tend to dynamically change.


Try something like this:

var partialId = 'WorldMap'; 

var container = document.querySelector('[id*="' + partialId + '"]'); 

if (container) {  

var containerId = container.id;  

}

You will have to change the partialId and make it dynamic of course, but this implementation might solve your issue.

2020-07-02 13-32-59
Max de Groot
Solution

Thanks for all the answers! 

I managed to resolve it by explicitly defining the name as attribute (kinda like Damian suggested). 


Then with this JS:


var elements = document.getElementsByName($parameters.ElementName);

var id = elements[0].getAttribute('id');

$parameters.ElementId_Out =  id;


I get back the required Id.


PS. I though it would be sufficient to specify the Name attribute already given by OutSystems, but this apperantly turn into the Id, making the name unavailable. 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.