cancel
Showing results for 
Search instead for 
Did you mean: 

sap.ui.getCore() --> byId() or getElementById()?

Former Member
0 Kudos

Hi, what is the better way to access a View-Control from the Controller by its ID:

sap.ui.getCore().byId() or sap.ui.getCore().getElementById()?

Where is the difference?

Accepted Solutions (0)

Answers (3)

Answers (3)

bradp
Active Participant

Hi Michael,

I think its best to avoid calling the .byId() function via sap.ui.getCore() as much as possible and rather call it via the object its created under. such as oController.byId("idButton"); or this.byId("idButton") or oView.byId("idButton");

See this section on Unique Id's in UI5 Developers Guide:

SAPUI5 SDK - Developers Guide - Support for Unique ID's

Cheers,

Brad

maximilian_lenkeit
Participant
0 Kudos

If you're in the controller, you shouldn't use neither of them (for the most part).

Use byId of the controller. It will resolve the generated id prefixes automatically.

- Max

Former Member
0 Kudos

So what should I do to access my Elements from the View in my Controller?

SergioG_TX
Active Contributor
0 Kudos

use

var yourControl =  sap.ui.getCore().byId("YourControlId"); 

Former Member
0 Kudos

Yes I know but

If you're in the controller, you shouldn't use neither of them (for the most part).

And I want to know what the alternative is if I should avoid this.

Former Member
0 Kudos

Both are the same & getElementByID() is deprecated. So, its better to use byId().

JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.core.Core