Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My innerHTML is coming out to be null despite using id or class or querySelector

This is the most frequent problem I come across, in this file was trying to access the innerHTML of the textarea using the same property.

But the error says that the input.innerHTML is null.

You can understand better by code,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import React, { Component, useEffect, useState } from 'react'
import './Body.css';
import axios from "axios";
import './Body.css'
export default function Navbar() {
    let noteArray;
    let input =  document.getElementById('.input_area') ;
    const AddNote = () =>{
        console.log("here" ,input.innerHTML) ; 
    }
    return (
        <>
            <div>
                <div className="input_info">
                    <textarea id='input_area' name=""  cols="50" rows="5">op</textarea>
                    <br />
                    <button onClick={AddNote} className='add'>Add Note</button>
                </div>
                <br />
                <div id='display_info' >
                    
                </div>
            </div>
        </>
    )
}

I tried from class, id, and querySelector but none of them is working.

Can anyone suggest me some edits?

>Solution :

To be sure that the elements of the component has been mounted to the DOM you need to execute the selection queries in useEffect hook.

import React, { Component, useEffect, useState } from 'react'
import './Body.css';
import axios from "axios";
import './Body.css'
export default function Navbar() {
    let noteArray;
    useEffect(() => {
       let input =  document.getElementById('.input_area');
       console.log("here" ,input.innerHTML);
    })
    
    const AddNote = () =>{
       let input =  document.getElementById('.input_area');
       console.log("here" ,input.innerHTML); 
    }
    return (
        <>
            <div>
                <div className="input_info">
                    <textarea id='input_area' name=""  cols="50" rows="5">op</textarea>
                    <br />
                    <button onClick={AddNote} className='add'>Add Note</button>
                </div>
                <br />
                <div id='display_info' >
                    
                </div>
            </div>
        </>
    )
}

Also, I suggest you use useRef instead of DOM select queries.

For example

import React, { Component, useEffect, useState, useRef } from 'react'
import './Body.css';
import axios from "axios";
import './Body.css'
export default function Navbar() {
    let noteArray;
    const textAreaRef = useRef(null)
    useEffect(() => {
       console.log(textAreaRef.current.innerHTML)
    })
    
    const AddNote = () =>{
       console.log(textAreaRef.current.innerHTML)
    }
    return (
        <>
            <div>
                <div className="input_info">
                    <textarea ref={textAreaRef} id='input_area' name=""  cols="50" rows="5">op</textarea>
                    <br />
                    <button onClick={AddNote} className='add'>Add Note</button>
                </div>
                <br />
                <div id='display_info' >
                    
                </div>
            </div>
        </>
    )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading