Getting this error at the moment:
Uncaught TypeError: Cannot read property 'value' of null
I call this in my render function below:
<input type="submit" className="nameInput" id="name" value="cp-dev1" onClick={this.writeData}/>
I also have tried calling it in here
componentWillMount: function(){ var name = document.getElementById('name').value; },
How can I get the id of an input text field and read that value and ensure it is not null?
I think the DOM is loading after I try to read the element hence why it is null
getElementById() method in React is using refs. To select an element, set the ref prop on it to the return value of calling the useRef() hook and access the dom element using the current property on the ref , e.g. ref. current .
To get the id of the element on click in React: Set the onClick prop on the element to a function. Access the id of the element on the currentTarget property of the event . For example, event.currentTarget.id returns the element's id .
The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
In React we can access the DOM element using Refs. Refs provide a way to access DOM nodes or React elements created in the render method. Creating Refs: Refs are created using React. createRef() and attached to React elements via the ref attribute.
The equivalent of document.getElementById in React is refs. We can assign a ref to an element and then retrieve the element that’s assigned the ref from the ref’s current property. to create a ref with the useRef hook and assign it to myContainer. Then we assign the ref to the div by setting the ref prop of the div to myContainer.
A recommended way for React 16.2 and earlier is to use the callback pattern: and so on... Show activity on this post. For getting the element in react you need to use ref and inside the function you can use the ReactDOM.findDOMNode method.
are you trying to access dom elements directly ? I dont recommend using dom elements directly in React but if you are using it anyways then you can do it in way use useRef in parent element then simply use querySelectorAll then use childs with index value of that array to access them directly
Before moving ahead let me inform you that React do not provide any way to access component through id. In html, we reference an element through id and javascript uses getElementById to access it. But, components of react are not html elements. In fact, they are not even the part of DOM.
You need to have your function in the componentDidMount
lifecycle since this is the function that is called when the DOM has loaded.
Make use of refs
to access the DOM element
<input type="submit" className="nameInput" id="name" value="cp-dev1" onClick={this.writeData} ref = "cpDev1"/> componentDidMount: function(){ var name = React.findDOMNode(this.refs.cpDev1).value; this.someOtherFunction(name); }
See this answer for more info on How to access the dom element in React
You may have to perform a diff and put document.getElementById('name')
code inside a condition, in case your component is something like this:
// using the new hooks API function Comp(props) { const { isLoading, data } = props; useEffect(() => { if (data) { var name = document.getElementById('name').value; } }, [data]) // this diff is necessary if (isLoading) return <div>isLoading</div> return ( <div id='name'>Comp</div> ); }
If diff is not performed then, you will get null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With