Attempting to use insertAdjacentHTML on a null property results in an error

I keep encountering the same error repeatedly... Can someone please explain what's going wrong here and provide a hint?

Error

script.js:20 Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null
at renderHTML (script.js:20)
at XMLHttpRequest.ourRequest.onload (script.js:13)

Javascript

var animalContainer=_('animal-info');
function _(id){

    return document.getElementById(id);

}

_('btn').addEventListener("click",function(){
    var ourRequest=new XMLHttpRequest();
ourRequest.open('GET','test.json');
    ourRequest.onload=function(){
        var ourData=JSON.parse(ourRequest.responseText);
        renderHTML(ourData);
        console.log(ourData[0]);
    };
ourRequest.send();
});

function renderHTML(data){
     animalContainer.insertAdjacentHTML('beforeend','testing 123');
}

index.php

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
</head>
<body>


    <button id ="btn">Submit</button>
<script type="text/javascript" src="script.js"></script>


<div id="animal-info"></div>
</body>
</html>

Answer №1

Ensure that the animalContainer variable is properly declared within the renderHTML() function, or passed as an argument when needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Django and Ajax to submit a post form

When utilizing ajax with Django, I have encountered an issue involving 2 forms. The first form collects the user's name and email, while the second form requires a confirmation code that is sent via email. def get_name(request): if request.method ...

Ending an $.ajax request when the page is exited

Currently, I have a function set on a timer to retrieve data in the background: (function fetchSubPage() { setTimeout(function() { if (count++ < pagelist.length) { loadSubPage(pagelist[count]); fetchSubPage(); ...

Utilize the assigned value of a variable from a separate file

Is it possible to set a variable in an external file called "Variable.js", assign it a value in a file named "Page1.html", and then use that variable with its assigned value in another file named "Page2.html"? For example, let's consider the contents ...

Arrays cannot be used with $addFields in MongoDB

I have encountered a challenge where I am dealing with a field that can be either a string or an array. How can I handle this scenario in the $addField query? Below is my MongoDB query code snippet: db.ledger_scheme_logs.aggregate([ { $match ...

What could be the reason for the esm loader not recognizing my import?

Running a small express server and encountering an issue in my bin/www.ts where I import my app.ts file like so: import app from '../app'; After building the project into JavaScript using: tsc --project ./ and running it with nodemon ./build/bin ...

Converting XML to a JSON object and then reverting back to XML using node.js

Looking to manipulate XML data using both node.js and angular.js. Once the conversion to JSON is complete, adjustments will be made before saving it back as an XML file. Priority must be placed on maintaining the sequence of child elements while also han ...

Personalized Svelte interface Slider tag

I am trying to customize the label of a smui/slider in a tick marks and discrete slider. While I found instructions on how to do this using material web components at https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc- ...

Is there a way to automatically update the URL to include $_GET['data'] (media.php?data=123) when a selection is made from a dropdown list?

I'm currently working on a project that involves a website in PHP and a MySQL database. I have successfully linked the two together, and now I have a combobox filled with data from the database. Below is my script for handling the selection: <scr ...

Understanding and parsing a JSON Schema using JSON.Net

I would like to use JSON.Net for deserializing this schema. { "color" : { "type" : "String", "description" : "What color would you prefer for your taco?", "required" : false, "default" : "Green", "options" : [ "Green", "Blue", ...

Changing route parameters or query parameters does not trigger a reload of component data in React

The "home" component has links that direct to the product component when clicked. Another component always visible displays links to recently visited products. However, on a product page, these links fail to work properly. Although the URL updates and tri ...

Issues with NextJS detecting environmental variables

I recently encountered an issue with my NextJS app running on Next.js v12.2.5 where it appears to be ignoring the environment variables I've configured. To address this, I created a .env.local file with the following content: NEXT_PUBLIC_SERVER_URL=h ...

What is the best way to modify a JSON file using PowerShell?

There's a json file named mytest.json, and I am looking to modify its values using a PowerShell script. The update.json file looks like this: { "update": [ { "Name": "test1", "Version": "2.1" }, ...

Retrieving JSON data through the Django API on an Android app

Currently, I am attempting to retrieve a JSON string using an API. The API I am using is custom-built on Django and is quite simple. When accessing /127.0.0.1:8000/get_last_data in a web browser, everything displays correctly. However, when trying to use ...

Show/hide functionality for 3 input fields based on radio button selection

I need assistance with a form that involves 2 radio buttons. When one radio button is selected, I want to display 3 text boxes and hide them when the other radio button is chosen. Below is the code snippet: These are the 2 radio buttons: <input type= ...

Button click initiates DataTables search rather than manually entering text in the input field

I am exploring the option of relocating the search function from an input to a button for a table that has been modified using DataTables. Currently, I have a customized input that triggers this function: <script> $(document).ready(function() { ...

Is it necessary to mark all checkboxes in the initial column of the gridview?

I have encountered an issue with my code that checks all checkboxes in the first column of a gridview. It seems to work only for Internet Explorer versions 5.0 to 8.0, but gives a Javascript error when running on IE 9 and above stating "Function Expected ...

Encountering null injector errors when running ng test on an Angular application

I have successfully developed a webpage in my Angular application and it is running perfectly. But, when I run ng test, some errors are popping up in Karma like the one shown in the image below: https://i.sstatic.net/lUKS5.png superuser.component.ts // ...

Unable to locate npm module called stream

For some reason, our tests have stopped running since yesterday. The error message reads: module stream not found Upon investigation, we discovered that 'stream' is available as a core node module: https://nodejs.org/api/stream.html#apicontent ...

What is the process for generating an Electronic Program Guide for television?

Welcome to the forum! I'm a front-end developer at a company for 6 months now, currently working on a TV app. It's my first experience in this field and I'm facing some challenges, particularly with creating an epg for the app. Unfortunately ...

Exploring Joomla and Filezilla for customizing contact forms and updating automatic responses

I'm facing an issue with my website. I've highlighted the specific area where the problem lies below. Here it is: " **result = '<p style="color:white;"><?php echo** JText::_('VG_SK_CONTACT_SUCCESS'); ?></p>' ...