Question
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
ING
BE
Last activity: 8 Sep 2015 10:24 EDT
How can I get a custom button's id in Pega 7?
Hello everyone,
I am using a javascript to capture an enter key event from the keyboard where I would like to simulate the behavior of a button click. The custom button is created within a section and I use the following js in a HTML tab of a different section...
<SCRIPT TYPE="text/javascript">
$(document).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode && keycode == '13'){
document.getElementById('button1').click();
}
});
</SCRIPT>
<div onKeyPress="return keypress(event)">
However, I am not able to locate anywhere the button id and hence the getElementById always return null (empty) or undefined id.
Anyone has encounter similar difficulty is Pega 7?
Thanks a lot in advance.
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
![](/themes/custom/pegacc_theme/images/user-icon.png)
![](/themes/custom/pegacc_theme/images/user-icon.png)
Hi all,
Just wanted to add a little to this thread. Test ID is privilege based so depending on the user's privilege the data-test-id attribute will be added to the control itself. Its main purpose is for automation so I would be hesitant to rely on its value for this scenario.
As another solution you could add a custom class to the cell that the button is inside of and then use querySelector to get a handle on it.
Then in code you can reference that button by doing something like.
I know its not ideal but this should work.
Thanks,
Max
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Pegasystems Inc.
JP
Can you share your user story about this Enter Key event?
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Citicorp Services India Private Limited
IN
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
ING
BE
Hi Gowrishankar,
Thanks for your response.
The events you have mentioned above are handled on elements such as button, text input, etc and you have to navigate/focus on that element and then if you hit the enter key the event will take its action.
However, this is not what our requirement is as I need to capture the enter key event from anywhere in my Section or Harness and then execute, for instance, a button click. The above js script works fine pega OOTB button, for instance, I am using the pxLocalAction Section where its button id is "button1", but on any custom button I create I dont know where or how to interact with the button's id.
I am using the below js to capture all the elements ids once you click on them on the Portal...
document.addEventListener('click', function(e) {
alert(e.target.id);
});
Now, this scripts always returns blank for my custom buttons whereas the ids of OOTB buttons are always returned with a value, which then I can refer to in order to use document.getElementById.
Thanks,
Ozkan
![](/themes/custom/pegacc_theme/images/user-icon.png)
![](/themes/custom/pegacc_theme/images/user-icon.png)
Hi Ozkan,
The recommended way to resolve this would be what Gowrishankar Ramasamy has mentioned in the above post. However, if your requirement is to still use 'document.getElementByID', you may try this: When you include a button control in a section, you have an option to generate an ID. You may use this ID in your script to get the reference to that button
Please let me know how this works for you.
Cheers,
Shan
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
ING
BE
Hi Shan,
I have already tried your suggestion above but it didn't work. It seems this is just a test id and the actual HTML id.
Thanks for your response.
Ozkan
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
TCS
US
Hi,
The Test ID option in OOTB configuration is actually the value of the field "Data-Test-ID" and not "ID" field as we expect it to be. You can see this if you view your source from your HTML page. I also have a similar requirement where I would need to show/hide Pega OOTB button based on a particular radio button selection.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Pegasystems Inc.
JP
How about configure a visible when rule on the button which need to be hide/show conditionally based on the radio button option?
You can refresh the section containing the button on the change event of your radio button.
Updated: 4 Sep 2015 4:00 EDT
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Pegasystems Inc.
JP
If you really try, it is possible to access the "Test ID" which was rendered as a custom html property "Data-Test-ID".
However, before writing any gory javascript, I would spend a little more time to challenge users to understand what is the real painpoint for them if the required feature was missing.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Pegasystems Inc.
JP
Sorry, I just noticed and corrected typo which may confuse reader in my comment above.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
TCS
US
Thanks!
I tried to have another control (saved from OOTB button) control and add an additional parameter ID which will set the Data-Test-ID from the default OOTB control for button, pxButton. This does not seek to work either. So just wondering what could be the way to access that data-test-id field.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
ING
BE
wondering and looking for the same. I can't find any document object method to interact with other HTML fields. document.getElementById refers specifically to the actual id of the element.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Pegasystems Inc.
JP
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
ING
BE
thanks for sharing Chunzi. Both solutions below worked fine for me but as Maxwell mentioned we should not rely on the test id...
$('[Data-Test-ID="20150812013413022024273"]').click();
document.querySelector("[Data-Test-ID='20150812013413022024273']").click();
Accepted Solution
![](/themes/custom/pegacc_theme/images/user-icon.png)
![](/themes/custom/pegacc_theme/images/user-icon.png)
Hi all,
Just wanted to add a little to this thread. Test ID is privilege based so depending on the user's privilege the data-test-id attribute will be added to the control itself. Its main purpose is for automation so I would be hesitant to rely on its value for this scenario.
As another solution you could add a custom class to the cell that the button is inside of and then use querySelector to get a handle on it.
Then in code you can reference that button by doing something like.
I know its not ideal but this should work.
Thanks,
Max
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
ING
BE
thanks for your help Maxwell. I will try this will post again the outcome.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
ING
BE
this worked too and I think it's the preferable option to go with. Mission accomplished!!!
Thanks a lot to all for your contribution on this issue.
Kind Regards,
Ozkan
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Pegasystems Inc.
JP
I am glad to hear that your mission accomplished
however I am still curious about why you need to implement such a global key event listener in your application?
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
TCS
US
Thanks Maxwell/Chunzhi