Conceal / Modify / Incorporate a picture

My ultimate goal is to have a hidden box and image that will be revealed based on the result of other functions. The box should change its background color, display an image, and switch images depending on the function called.

I am facing two issues with the code provided below:

  • Initially, the box appears but the images do not show up when the code is executed.
  • When either function is called a second time (after working fine without errors on the first call) by the rest of the script (such as in a repeated calculator scenario), an error occurs implying that the image no longer exists:

"JavaScript runtime error: Unable to set property 'src' of undefined or null reference"

JScript:

function one(input) {
    document.getElementById("result").style.background = "lightgray";
    document.getElementById("result").style.color = "green";
    document.getElementById("icon").src = "tick.png";
    document.getElementById("icon").style.visibility = "visible";
}
function two(input) {
    document.getElementById("result").style.background = "lightgray";
    document.getElementById("result").style.color = "red";
    document.getElementById("icon").src = 'cross.png';
    document.getElementById("icon").style.visibility = "visible";
}

HTML:

<div id="result">
<img src="tick.png" id="icon" style="visibility:hidden">
</div>

And CSS (though may not be necessary):

#result {
    width: 820px;
    height: 450px;
    border-radius: 20px;
    padding-top:25px;
    padding-bottom: 25px;
    color: green;
    font-size:40pt;
    text-align: center;
    margin-top: 100px;
    margin-right: 150px;
    margin-left:420px;
}

#result img{
    margin-top: 85px;
    width:220px;
    height: 190px;
}

Answer №1

Thanks to Barmar's suggestion, it got me thinking and I appreciate that!

I realized after examining the DOM while calling the functions that another part of the script using .innerText was replacing the code in the HTML (still not sure why). As a result, on the initial call, my tag disappeared, leading to an attempt to call a null reference. Placing the tag in its own div solved this issue.

In addition, adjusting the position attribute to absolute for the tag in the CSS brought the image back into view, resolving that problem as well.

Thank you all for your assistance, it means a lot! :)

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

ways of exporting and using arrays in Node.js

Constantly receiving the "undefined" error in main.js: var dbAccess = require('../dao/dbAccess'); dbaInstance = new dbAccess(); var wordPool = dbaInstance.getWordPool(); console.log (wordPool); The contents of "dbAccess.js" are as follows: var ...

Adjust the size of the mouse cursor in real time

I'm currently in the process of developing a project where I want to create a web application with a mouse cursor that appears as a circle with a customizable radius, which can be altered by the user. The main requirement is for this custom cursor to ...

Centering <th> elements is key for achieving a visually appealing print layout

Is there a way to center align the header and body of a table when printing it? I am using datatable. Here is how it currently looks: Check out this screenshot I have tried adding classes to the th tags in the HTML table, but they still appear aligned t ...

What are the steps to incorporate PointerLockControl in Three.js?

Having trouble correctly integrating the PointerLockControl in Three.js? Despite trying various examples, errors seem to persist. I've been importing libraries through the head part like this: <script src="lib/controls/PointerLockControls.js"> ...

Using JavaScript, add a complex JSON record to a JSON variable by pushing it

I have been attempting to insert a complex JSON data record into a JSON variable using the following code: var marks=[]; var studentData="student1":[{ "term1":[ {"LifeSkills":[{"obtained":"17","grade":"A","gp":"5"}]}, {"Work":[{"obtained":"13"," ...

Is there a way to incorporate jQuery into SharePoint 2010?

I am encountering an issue with linking my HTML page to our organization's SharePoint 2010 portal. All necessary files (CSS, images, jQuery) are stored in the same document library. While the CSS is functioning properly, I am facing challenges with ge ...

What is the most effective way to retrieve 'this' within an object method that is being used as a callback function?

I am currently working on a word game project to enhance my understanding of JavaScript, especially since I am new to it. To facilitate user interaction, I am utilizing the NPM package prompt (https://www.npmjs.com/package/prompt). Coming from an OOP back ...

Div with CSS shadow on all sides

Is there a way to achieve this effect using only CSS (not CSS3) so that it is supported by all browsers? I am looking to create a div with shadows on all sides, where the top area will be used for navigation. I have tried searching for tutorials but haven ...

Creating a dynamic height sticky sidebar and footer in Bootstrap 5 using CSS/HTML

Looking to implement a sticky sidebar feature with a header and footer that remain visible while the middle content scrolls. The overall height of the page will vary, so I plan to use Bootstrap 5 grid for structuring. <!DOCTYPE html> <html la ...

The mesmerizing world of parallax animations and the smooth scrolling experience

I recently built a website using the SUPERSCROLLORAMA plugin, only to discover later that there are issues with parallax scrolling on iPad and iPhone. Now I'm trying to figure out how to solve this problem. It seems that events are disabled on these ...

Disable link 2 when link 1 is clicked

Looking to create a feedback form with two exclusive links. Want to ensure that if someone clicks the first link, they cannot click the second link and vice versa. Interested in exploring options like using cookies to prevent multiple clicks or possibly ...

What is the method for ensuring a variable returns a number and not a function?

I've encountered a perplexing issue that I can't seem to solve. What could be causing the code below to output a function instead of a number? let second = function(){ return 100 }; console.log(second); ...

How can I invalidate the Authorization header in a request?

After delving into the Basic Authentication algorithm and successfully implementing it, I've encountered an issue. The Authorization header in the request seems to never expire, allowing the client to continuously access protected content without re-a ...

Tips for avoiding html entities in a string

To prevent any user-entered content from being interpreted as HTML, I need to escape it so that special characters like < become < in the markup. However, I still want to wrap the escaped content with actual HTML tags. The goal is to ensure that the ...

Transforming video files into HTML5 ogg/ogv and mpg4 formats

Is there a reliable software out there that can effectively convert video files (such as avi, flv, etc.) to HTML5 supported ogg/ogv and mpeg4 formats? I have tested a few options but unfortunately none of them seem to get the job done correctly. ...

Issue with SweetAlert2 cancel button being unresponsive while an ajax request is in progress

I have a custom function triggered when a dropdown item is selected, which triggers a sweetalert2 popup. Here's the function: function SwalPopup(ip, method) { var methodmsg = method.charAt(0).toUpperCase() + method.slice(1); swal.fire({ ...

What is the best way to include the application version in an Electron project using JavaScript

While attempting to publish an update for my app, I encountered a strange error. Can anyone pinpoint the issue? (Note: Node.js is included) Error Message: Unexpected token < <script> console.log(process); let output = <h2 class="page- ...

setting the child div's margin in relation to its parent div

Here is my HTML: <div class='mainDiv'> <div class='subDiv'></div> </div> This is the corresponding CSS code: .mainDiv { margin-top: 200px; width: 700px; height: 800px; background-color: red ...

The db.all method does not provide an array as its return variable

Seeking to extract an array from the db.all function to incorporate it into a custom JSON object for a response. Various attempts have been made including using Object.values(members), members.fulfillmentValue, and some PROMISE methods discovered on Stack ...

What is the method to remove the overlay from view?

Can anyone help with a small issue I'm having? When I click on button one, a modal popup appears but the overlay doesn't disappear when unloading. I've tried the code below, but can someone suggest what might be causing the problem? var po ...