Utilize Javascript to conceal images

On a regular basis, I utilize these flashcards. Recently, I have started using images as answers. However, I am facing an issue - I am unable to conceal the pictures. My preference is for the images to be hidden when the webpage loads.

function myShowText(id) {
  document.querySelector('#' + id + ' .answer').style.color = 'black';
}

function myHideText(id) {
  document.querySelector('#' + id + ' .answer').style.color = 'white';
}
.answer {
  border-style: solid;
  border-color: #287EC7;
  color: white;
}
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8>
  <title> Flashcards VBA </title>
  <link rel="stylesheet" href="css/styles.css">
</head>

<body>
  <script src="js/scripts.js></script>
  <h3> Flashcards </h3>
  <p class="question">
    The primary question
  </p>

  <div id="bash_start"><
    p class="answer">
      img src="image.jpg">
    </p>
  </div>
</body>
</html>

Answer №1

To implement hiding and showing content with CSS, you can use the following steps:

.invisible {
    visibility: hidden;
}

Next, apply the class .invisible to the content you want to hide:

<div class="content invisible">
    <p>Some hidden text</p>
</div>

Then, you can reveal the content by removing the .invisible class dynamically with JavaScript:

document.querySelector('.content').classList.remove('invisible');

Here is an example demonstrating this functionality:

var btn = document.querySelector('button');
var content = document.querySelector('.content');

btn.addEventListener('click', function() {
  content.classList.remove('invisible');
});
.invisible {
  visibility: hidden;
}
<button type="button">Show Content</button>

<div class="content invisible">This content will be revealed upon button click.</div>

Answer №2

If you're looking to hide your image when the page loads, you can simply add some CSS to achieve that. Just use visibility: hidden; or display: none; on your image(s).

Then, on the JavaScript/jQuery side, you can trigger any event you want and change the visibility back to visible; or block/inline-block;.

<img class="flashCards" src="https://cdn4.iconfinder.com/data/icons/champions-1/512/Champions-04-512.png">
<button id="validate_btn" onclick="validate()">Validate</button>

<style>

img.flashCards { width: 150px; visibility: hidden; }
</style>

<script>
function validate() {
  var flashCards = document.getElementsByClassName('flashCards');
  //Need a for loop for each image element
  //If only one image use getElementById directly with the style
  for(i=0; i<flashCards.length; i++) {
    flashCards[i].style.visibility = 'visible';
    flashCards[i].style.backgroundColor = 'green';
  }
}
</script>

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

Angular's route resolve feature does not wait for the promise to resolve before

I just started using angular and I'm facing an issue with my user route. I'm trying to resolve the user object before rendering the view, but even after injecting $q and deferring the promise, the view is loading before the promise gets returned. ...

Tips for concealing the Google Chrome status bar from appearing on a webpage

I have been intrigued by the rise of Progressive Web Apps (PWAs) and I am eager to dive into understanding them better. One common feature I have noticed in PWAs is the ability to hide the browser chrome, including the URL bar, back button, search fields, ...

Changing the cursor to a waiting state following a mouse click

Looking for some help here - when I click a button, the cursor remains standard and doesn't show loading. Any suggestions on how to fix this? Below is the snippet of my code: <div class="signup"> <input type="submit" value="Sign Up" tit ...

Using setInterval in JavaScript to automatically update a TextField

As someone who is relatively new to Javascript and jQuery, I am trying to make a simple code that updates a text field with random values every 5 seconds. However, my implementation does not seem to be working. I apologize if this question seems too basic ...

The Vue.js route is not aligning with its defined path, causing a mismatch

Attempting to develop a Vue SPA app, but encountering an issue with the routes not matching what was defined in the router. Despite all configurations seemingly correct, there is confusion as to why this discrepancy exists. What element might be overlooked ...

Protractor - selecting a hyperlink from a list

Imagine you have a todo application with tasks listed as follows: Walk the dog, Eat lunch, Go shopping. Each task has an associated 'complete' link. If you are using Protractor, how can you click on the 'complete' link for the second t ...

Having trouble getting Angular-UI-Select to work with a basic variable on $scope?

How can you reset an array with selected values so that the values can be reselected from the dropdown? I am working with a people array where the values are initially displayed in a select dropdown. When I choose certain names, they get transferred to th ...

Looking for a way to retrieve Instagram data with Python using Selenium, following Instagram's recent API changes? Having trouble locating all entries, as only 12 are showing up?

I am currently working on scraping Instagram using Python and Selenium with the objective of extracting the URL of all posts, number of comments, number of likes, etc. Although I have successfully scraped some data, I have encountered an issue where the p ...

Utilize jQuery to create a dynamic image swapping and div showing/hiding feature

I'm having trouble implementing a toggle functionality for an image and another div simultaneously. Currently, when the image is clicked, it switches to display the other div, but clicking again does not switch it back. Can someone please advise on wh ...

Achieving Inline Icons with Text in Bootstrap 5 Button

Looking for some assistance <div class="col-auto mt-lg-0 mt-2"> <a href="#" style="--bs-btn-font-size: .95rem;" class="btn btn-outline-secondary"> <i class="bi bi-download"></i&g ...

What could be causing bundle.js to remain in a pending status on my website?

Whenever I try to open a page from my project, the browser keeps showing loading forever and in the network tab, the status of bundle.js is pending. (Interestingly, if I open other routes of the project, everything works fine). If I remove all product var ...

Make the header background color shift as the page is scrolled down on the fixed header

I am currently using a sticky transparent header on my website www.obviagency.com Here is the CSS code: #site-header-inner { height:0px; z-index:170; margin:0 auto; width:100%; position:fixed; top:0; margin-top:10px; } I would like to change the backgr ...

"Invalid operation" error encountered within a Flask function in Python

I am trying to store a 'person' resource in a .ttl file using a JavaScript function: Here is my SPARQL query: @app.route('/registraAnnotatore/<author>+<mail>', methods=['POST']) def registraAnnotatore(author, ...

Concealed checkbox value in jQuery

I have a problem with setting the hidden checkbox "marketingPhone" to TRUE when the "marketingAAA" checkbox is checked as true. This part works fine. However, if any other checkbox on the page is set to TRUE, then it also sets "marketingPhone" to TRUE. I ...

When attempting to submit information to a database table, I encounter an error in the browser console: "Uncaught (in promise) Error: Request failed with status code 404."

In my React Node project's login page, I am encountering the following issue. Despite thoroughly reviewing my code after each execution, the error persists. My goal is to pass data from the login page into my MySQL database; this marks the initial sta ...

The typeof operator is not functioning as anticipated when used with an ajax response

Trying to implement form validation using AJAX and PHP, but encountering issues with the typeof operator. The validation works smoothly except when receiving an empty response, indicating that all fields have passed. In this scenario, the last input retai ...

Searching in the Kendo Dropdown List using text and value

$(document).ready(function() { $("#selection").kendoDropDownList({ filter: "startswith", dataTextField: "SelectionName", dataValueField: "SelectionID", dataSour ...

javascript utilize bluebird promise to asynchronously retrieve the first file from a given list

I am currently working on implementing promises in JavaScript to retrieve the first available file from a list of paths (for example, ["c:\\temp\\test1.json", "c:\\temp\\test2.json"]). The goal is to identify and ret ...

Can you suggest a method to save cookies or HTML5 localStorage across multiple WebBrowser instances on Windows Phone?

Quick question: How can I save data from Javascript in a WebBrowser control on my Windows Phone 8 app so that it remains even after the user closes and reopens the app? In more detail: I am working on a Windows Phone 8 app with a single WebBrowser contro ...

Maintain the image's aspect ratio by setting the width equal to the height when the height is

I am uncertain if achieving this purely with CSS is possible, but it would be ideal. I currently have an image: <img src="#" class="article-thumb"> CSS: .article-thumb { height: 55%; } Is there a way to set the width equal to the height? My g ...