How to get an Element by ID in ReactJS ?
Last Updated :
23 Jul, 2025
ReactJS, a popular JavaScript library for user interfaces, empowers developers with tools, such as the ref system, to manipulate the DOM by accessing elements using their unique IDs. This article explores the process of obtaining elements by ID in ReactJS for effective component interaction and manipulation.
Several methods can be used to Get an element by ID in React JS, which are listed below:
Prerequisites:
Steps to Create React Application:
Step 1: Create a react application by using this command
npx create-react-app getelementID-app
Step 2: After creating your project folder, i.e. getelementID-app, use the following command to navigate to it:
cd getelementID-app
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Approach 1: Using document.getElementById()
In this approach, elements are retrieved by their unique IDs using the document.getElementById() method. It is a standard DOM function that proves effective.
Syntax:
const element = document.getElementById('yourElementId');
Example: Below is the code example of the above approach.
JavaScript
import React, { Component } from 'react';
class App extends Component {
handleClick = () => {
const element = document.getElementById('myButton');
if (element) {
// Manipulate the retrieved element's style
element.style.backgroundColor = 'green';
element.style.color = 'white';
element.style.border = 'none';
element.style.padding = '10px 20px';
element.style.borderRadius = '10px';
element.style.cursor = 'pointer';
// Add more styling properties as needed
}
};
render() {
return (
<div style={styles.container}>
<h1 style={styles.heading}>
Geekforgeeks
</h1>
<button id="myButton"
onClick={this.handleClick}>
Click me
</button>
</div>
);
}
}
export default App;
const styles = {
container: {
textAlign: 'center',
margin: 'auto',
padding: '20px',
width: 400,
}
};
Steps To Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
Approach 2: Using Refs
In this approach, in React involves creating a ref using React.createRef() and attaching it to an element through the ref attribute. Although not as common, it provides direct access to DOM nodes while keeping them encapsulated within React components.
Syntax:
const node = this.myCallRef.current;
Example: Below is the code example of the above approach.
JavaScript
import React, { Component } from 'react';
class RefsExample extends Component {
constructor(props) {
super(props);
this.myButtonRef = React.createRef();
}
handleClick = () => {
if (this.myButtonRef.current) {
// Manipulate the retrieved element's style
const buttonElement = this.myButtonRef.current;
buttonElement.style.backgroundColor = 'green';
buttonElement.style.color = 'white';
buttonElement.style.border = 'none';
buttonElement.style.padding = '10px 20px';
buttonElement.style.cursor = 'pointer';
buttonElement.style.borderRadius = '10px';
// Add more styling properties as needed
}
};
render() {
return (
<div style={styles.container}>
<h1 style={styles.heading}>
Geekforgeeks
</h1>
<button ref={this.myButtonRef}
onClick={this.handleClick}>
Click me
</button>
</div>
);
}
}
export default RefsExample;
const styles = {
container: {
textAlign: 'center',
margin: 'auto',
padding: '20px',
width: 400,
}
};
Steps To Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
Approach 3: Using React State
In this approach, React's state mechanism is employed to manage element styles. When the button is clicked, the state is updated, modifying the button's appearance. This maintains a declarative pattern and simplifies handling dynamic styling within React components.
Syntax:
this.setState((prevState, props) => ({
counter: prevState.count + props.diff
}));
Example: Below is the code example of the above approach.
JavaScript
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
buttonStyles: {
backgroundColor: 'green',
color: 'white',
border: 'none',
padding: '10px 20px',
cursor: 'pointer',
borderRadius: '10px',
},
};
}
handleClick = () => {
this.setState({
buttonStyles: {
backgroundColor: 'red',
color: 'white',
border: 'none',
padding: '10px 20px',
cursor: 'pointer',
borderRadius: '10px',
},
});
};
render() {
return (
<div style={styles.container}>
<h1 style={styles.heading}>
Geeksforgeeks
</h1>
<button
style={this.state.buttonStyles}
onClick={this.handleClick}
>
Click Me
</button>
</div>
);
}
}
const styles = {
container: {
textAlign: 'center',
margin: 'auto',
padding: '20px',
width: 400,
},
heading: {
fontSize: '24px',
marginBottom: '10px',
},
};
export default App;
Steps To Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
How to get an Element by ID in ReactJS ?
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsIn React, forms are used to take input from users, like text, numbers, or selections. They work just like HTML forms but are often controlled by React state so you can easily track and update the input values.Example:JavaScriptimport React, { useState } from 'react'; function MyForm() { const [name,
4 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects