Display a Div when an image is clicked using JavaScript

I am attempting to create a clickable image that reveals a div when clicked. The catch is that the div can only be shown by clicking the image, not hidden again. Clicking on another image will replace the existing div with a new one.

I was able to achieve this using the following code:

function myFunction() {
  document.getElementById("popis").innerHTML = "Hello World";
}

function myFunction2() {
  document.getElementById("popis").innerHTML = "text";
}

function myFunction3() {
  document.getElementById("popis").innerHTML = "Nejaky text ";
}
img {
  height: 150px;
}
<img src="https://clipart.info/images/ccovers/1495916678star-png-red-small-alpha-transparent-image-clip-art.png" onclick="myFunction()"></button>
<img src="https://clipart.info/images/ccovers/1495916678star-png-red-small-alpha-transparent-image-clip-art.png" onclick="myFunction2()"></button>
<img src="https://clipart.info/images/ccovers/1495916678star-png-red-small-alpha-transparent-image-clip-art.png" onclick="myFunction3()"></button>
<div id="popis"></div>

This code changes the text displayed when an image is clicked. However, I have been unable to make it work with divs. I want to include headings and text paragraphs within divs, not just standalone paragraphs. Ideally, I would like one div to be shown by default and then switched when clicking on another image.

Next, I need to integrate this functionality into WordPress. Fortunately, since I already have images and a div section set up, I believe implementing this code should be straightforward.

Although using toggle or arrays may be possible, I have struggled to make them work in this context recently. Nonetheless, I did manage to see some results while experimenting with these approaches.

Answer №1

To optimize your display of data, consider creating individual div elements for each set of information you wish to present. You can utilize CSS to hide these dynamic divs by default using the rule display: none;. Whenever you are ready to reveal them, you have the option to use vanilla JavaScript with

document.getElementById("myDiv").style.display = "block";
, or employ jQuery and simply write $('myDiv').show();.

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

Preventing links from functioning on dynamically generated web pages

I have implemented the following code to deactivate all links on a preview page: var disableLink = function(){ return false;}; $('a').bind('click', disableLink); While this successfully disables all static links, any anchor tags loade ...

I am experiencing an issue with my jQuery loop code not functioning properly when using the .each method within the loop

I am struggling with the following code. <input type="text" name="1" class = "inp<?=$p?>"> <input type="text" name="2" class = "inp<?=$p?>"> <input type="text" name="3" class = "inp<?=$p?>"> <input type="text" na ...

Having trouble retrieving response content in Mithril

I've been experimenting with making a request to a NodeJS API using the Mithril framework in my client application. I attempted to fetch data by following their example code: var Model = { getAll: function() { return m.request({method: "G ...

Fetching image URL via an AJAX request with CarrierWave

When using an API requested through ajax, the query returns a collection that is serialized into json before being sent back to the client. Each record in the collection has a photo URL assigned by Carrierwave. This seems like a standard process. However, ...

The jquery ajax upload component fails to function properly in Internet Explorer when used with a tapestry framework

Issue with jQuery j:ajaxUpload in Tapestry - File upload not working in IE To replicate the issue, follow these steps: Visit Click on the "Example" tab. Attempt to upload a jpg, gif, or png file. In Internet Explorer, the file size displays as 0, while ...

What is the method for entering text into alerts with Protractor?

Attempting to access a webpage with a login alert requesting credentials. My search for how to switch to the alert box online has proven fruitless. How do I enter the username and password? In my previous attempt to automate this page using Selenium WebDr ...

Effortlessly submit multiple forms at once with just a single click using the WordPress form

One issue I'm experiencing is that the forms on my site are generated using shortcodes. I have a suspicion that the buttons I created, which lead to submission form1 and then form2, may not be working properly because of this. This is the current set ...

Change the date string to year, month, and day

When using Ajax's getResponseHeader("Last-Modified"), it returns a date string with the format displayed below: Thu Oct 13 2016 13:05:17 GMT+0200 (Paris, Madrid, sommartid) I am wondering if it is achievable in javascript to extract the year, month, ...

Apply jQuery styling to new select box on page in order to maintain consistent styling throughout

I've encountered an issue with my jQuery select box styling. It appears to work perfectly on the initial page load, but when new content containing a select box is dynamically loaded onto the page, the styling doesn't get applied to it. Can anyo ...

Tips on saving content that changes automatically

I am curious about the best way to store dynamically displayed HTML content. For example, when a certain element is clicked on a website, I want text to appear elsewhere on the page. One idea I had was to create a variable in a JavaScript/jQuery script to ...

Customizing Bootstrap Modal's backdrop CSS for a specific element

Upon opening a bootstrap modal, the background takes on a darker shade as seen here. I am looking to apply a CSS class that mimics this shading effect to a specific element. I prefer not to use any JavaScript for this task as it is purely about styling wi ...

What are the steps to program a bot to respond to different types of media messages (such as pngs, mp4

I have been attempting to elicit a reaction from him based on the media message, but my attempts have been fruitless so far. It seems that the only time it reacts is when there is no text within the message... which complicates things. Can anyone provide s ...

Utilizing visual elements to change the options of a dropdown menu

I am currently working on creating a form for a client who has requested a map of the city to allow visitors to visually select their location. They want this feature to link to the City Dropdown/Select area of the form. Here is a link to the client' ...

Update the variable in a PHP script and execute the function once more without the need to refresh the page

Hey there, I'm wondering if AJAX is necessary for the functionality I want to implement. Here's the scenario: I have a function that generates a calendar and I plan to add 'forward' and 'backward' arrows so users can navigate ...

Using double quotes when adding HTML5 local storage entries

Currently, I am setting the value of a field to a variable like this: newUser = document.getElementById('userName').value; Then putting that variable into local storage: localStorage.setItem('userName', JSON.stringify(newUser)); Fet ...

Is there a way to automatically fill up a second dropdown menu based on the user's selection from the first dropdown

Using jQuery or vanilla JavaScript, I am looking to dynamically populate a second dropdown based on the selection made in the first dropdown. The value chosen in the first dropdown will determine which options are displayed in the second dropdown. What is ...

Is it possible to send an HTTP request from the renderer process to the main process in Electron?

Currently, I am in the midst of developing an electron video player project. My main objective is to stream video from a readable stream to an HTML video-tag. I am exploring different solutions and strategies to achieve this goal. In particular, I am cur ...

Why is MutationRecord[] organized as an array in MutationObserver?

I've been diving into the world of MutationObserve, and I've grasped most of it. However, one thing that's puzzling me is why the parameter requires an array. According to the documentation: An array of MutationRecord objects, detailing e ...

Difficulty transferring function to child element

It seems like I may be overlooking something simple due to exhaustion, but I'm hoping someone can assist me! I have a function that the parent component inherits from the provider, and I am trying to pass it to the child as shown below: console.log(t ...

Unable to send message using window.parent.postMessage when src is set to "data:text/html;charset=utf-8" within an iframe

I'm currently working on a project to develop a compact editor that allows users to edit HTML using an iframe. I'm passing an HTML string into the src attribute as data:text/html, but I'm encountering challenges with communication in the par ...