Is there a way to modify the visibility of an element on a webpage once an image element is clicked or activated?

I've been experimenting with changing the opacity of a div element when you click on an img element using JavaScript or jQuery. I'm not sure if this is achievable with CSS or HTML alone, so if you have any insights, please share! Here's the current code I'm working with:

<div class="scene" id="scene">the text</div>
<!--the text element I'm attempting to modify-->
<img src="zyro-image (7) (1).png" id="look" class="look"><img>
<!--the img element; clicking it changes the div element's opacity-->

Styles ⬇️

.scene{
    font-family: 'Common Pixel', sans-serif;
    background-color:black;
    border: 2px;
    color: white;
    padding-top: 10px;
    padding-bottom: 25px;
    width: 350px;
    opacity: 0;
}

<!--____________________-->

.look{
    height: 100px;
    position: relative;
    left: 10px;
    top: -685px;
    margin-top: auto;
    z-index: 1;
    transition: top, 1s;
}

.look:hover{
    top: -692px;
}

Here are some methods I've attempted:

(These ideas were gleaned, if not entirely, from stackoverflow)

1.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
$('.look').active(function(){
  $('.scene').css('opacity', '1');
 });
</script>

This approach didn't seem to work; no visible change occurred.

2.

<script>
    function myFunction()
    {
        document.getElementById('scene').setAttribute("class", "scene2");
    }
</script>

<styles>
.scene2{
    font-family: 'Common Pixel', sans-serif;
    background-color:black;
    border: 2px;
    color: white;
    padding-top: 10px;
    padding-bottom: 25px;
    width: 350px;
    opacity: 1;
}
</styles>

Similar to the previous attempt, there was no noticeable change.

3.

<script>
        function change_css(){
            document.getElementById('result').style.cssText = 'font-family:'Common Pixel', sans-serif; background-color:black; border:2px; color:white; padding-top:10px; padding-bottom:25px; width:350px; opacity:1;';
        }
    </script>

Unfortunately, this method also failed to produce the desired effect.

Answer №1

To implement a change in opacity, add an onclick eventlistener to your image:

<img src="yoursrc" onclick="changeOpacity()"/>

Assign an id to your div:

<div id="mydiv">I'm a div</div>

Next, use the following function to select your div by id and apply the 'opacity-low' class:

function changeOpacity() {document.getElementById('mydiv').classList.add('opacity-low')}

CSS Class:

.opacity-low {opacity: 0.5;}

There may be alternative methods to achieve this effect, but I am not proficient in plain JavaScript.

Answer №2

Adjusting opacity of DIV upon clicking on IMG:

$("#img").click(() => $("#div").css("opacity", 0.5))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="div">Make me semi-transparent</div>
<img id="img" src="https://via.placeholder.com/100" />

Another approach using CSS

<style>
   .opacityClass {opacity:0.5;}
</style>

...
$("#img").click( () => $("#div").addClass("opacityClass") )

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

Having Trouble Accessing Custom Screen in React Navigation?

The navigation from the Order screen to the Home Screen is not working as expected. Every screen in the route works, except for the Home screen, which just navigates back to the Map screen. I have clearly instructed to navigate to Home. Here is the curren ...

Prevent a visitor from submitting the form again if it has already been submitted by the same visitor

Is there a way to prevent a visitor from submitting a form more than once? I've been thinking that cookies might be the solution. In my controller, I implement a cookie like this: class RatingsController < ApplicationController require 's ...

Tips for properly nesting a "carousel" div within an "oe_structure" without any overlapping issues

Having trouble with placing a "carousel" div inside the "oe_structure" in my odoo template. I am currently working on editing the "website_sale.products" template. As a newcomer to Odoo, can I use inheritance in my direct code to make edits? I'm stil ...

What is the best way to access elements of a certain class that begin with a specific string?

Here's the scenario: I have a group of elements that each have multiple classes assigned to them. Take a look at this example: <input class="etape btn-info others"> <input class="etape btn-inverse others"> <input class="etape btn-dange ...

Verify whether the element in the DOM is a checkbox

What is the method to determine whether a specific DOM element is a checkbox? Situation: In a collection of dynamically assigned textboxes and checkboxes, I am unable to differentiate between them based on their type. ...

Attempting to send an Ajax request to add an email to a MailChimp mailing list, but encountering an

Apologies for my poor English, I am new to Ajax. I attempted to create an Ajax function to add emails to a MailChimp list when the subscribe button is clicked, but I discovered that the URL in my Ajax code was incorrect. Below are the details: My Contro ...

Why isn't the Bootstrap carousel showing up on my webpage?

I have been attempting to incorporate a bootstrap carousel into my project. However, upon inspecting the element, I can see the div and code, but no images, buttons, or any other content are visible. If anyone could take a look and offer assistance, it wo ...

Syntax for the "data" parameter in jQuery.ajax

I am attempting to send the contents of a JavaScript variable to the server for further processing. While I have no issue passing static strings, I encounter a problem when trying to pass a variable containing a string as the WebMethod fails to execute. He ...

Which method is best for extracting content from a WebView - using Regx, implementing JQuery, or utilizing HTML parsing?

Criteria for Acceptance I am working with a website in a WebView that is not under my ownership. I want to be able to trigger native button clicks based on the content of the page. Tasks at Hand I need to determine if the HTML content contains specific UR ...

Interactive navigation feature developed using VueJS

Attempting to implement a reactive navigation system that changes based on user authentication status. Following a login action in the application, a token is stored in local storage. If this token exists, I aim to display a logout button. Despite trying v ...

Using the ternary operator along with equality statements within ng-class

I'm trying to enhance an element with the following ng-class attributes: <div ng-class="a ? 'class1' : 'class2'"></div> Now, I want to include another ng-class statement: ng-class="class3: b !== c". How can I merge thes ...

Is there a way to prevent ng-template-loader from scanning image src's?

Currently, I am beginning to incorporate webpack into my development workflow for an angular project. To create my templateCache, I have had to include the ng-template-loader. Below is a snippet of my webpack configuration: { test: /\.html$/, loa ...

Tips for modifying the color scheme of an SVG image with CSS

I am in the process of changing the color of an SVG using CSS. The code I attempted below only alters the background color, but I also need to change the text color. This is my original SVG. https://i.sstatic.net/7lNE0.png The desired output should look ...

Steps to set up datetimepicker with varying date ranges for several input fields

I am having trouble getting a new date range for each text box in my form. It seems to only return the date range from the last textbox, even though I have made the textbox IDs dynamic. I have both start and end dates for each textbox and I have calculated ...

What causes useEffect to be render-blocking (paint-blocking) in the tooltip demonstration?

Following the tooltip example for the useLayoutEffect hook from the updated react docs, I have concluded the sequence of events as follows: react render() --> reconciliation --> update DOM if virtual dom is changed --> DOM update finishes --> ...

Aligning msform using CSS

Can someone assist me with centering the form (#msform, #msform fieldset) on all browsers while also keeping the image within the border and ensuring that the form does not extend beyond the right border? JSFiddle #msform { width: 1200px; margin: ...

calculate the product of the table's td element

Recently, I encountered a task where I had to make a table with an editable quantity cell. Whenever the quantity was changed, it needed to be multiplied by the value in another parent td. Here is the code snippet that helped me achieve this: $(document) ...

Establish a connection between a online form and a MySQL database

When working with a MySQL database, the process of adding users to the system involves inputting test data as shown below: INSERT INTO `database_users`.`Members` (`ID`, `Username`, `Password`, `First Name`, `Last Name`, `Email Address`, `Telephone Number ...

Text styling in HTML using a gradient effect

In the game LoR, players have the ability to edit their deck names using HTML code. As someone with no experience in HTML, I've noticed some players with gradient deck names and I'm struggling to figure out how they achieved this effect. The prob ...

Utilizing a TypeScript Variable as a Tagname in an HTML File within Angular

This specific problem is originally documented in this post. Despite being flagged as a duplicate, my scenario differs because the HTML content at hand is too extensive for utilizing innerHTML. The structure of my component's HTML file is as follows: ...