The dimensions of the div do not match the size of the image

I am struggling to adjust the color of a transparent image within a div using jQuery. I am facing difficulties in keeping the image aligned within the div while changing its color.

Can someone provide assistance? My goal is to have the image filled upon a user clicking on any of the span colors.

custom-styles.css:

.iconWrapper .color span
{
    width: 50px;
    height: 58px;
    display: block;
}
.color1
{
    background-color: #f6f6f6;
}
.color2
{
    background-color: #ffe25b;
}
.color3
{
    background-color: #e35990;
}
.color4
{
    background-color: #58c1d8;
}

step1.php

<link rel="stylesheet" type="text/css" href="custom-styles.css" media="screen" />
<script language="javascript" type="text/javascript">

    $(function() {
        $('.iconWrapper span').click(function(e){
        var color=$(this).attr('class');
            //alert(color);
            $('#div1').removeClass().addClass(color);
            $('#hiddencolor').val(color);
            e.preventDefault();
        });    
    });    
</script>
</head>
<body>
    <div class="iconWrapper">
        <ul class="color">
            <li>
                <a href="#" title="Select"><span class="color1"></span></a>                               
            </li>
            <li>
                <a href="#" title="Select"><span class="color2"></span></a>                               
            </li>
            <li>
                <a href="#" title="Select"><span class="color3"></span></a>                               
            </li>
            <li>
                <a href="#" title="Select"><span class="color4"></span></a>                               
            </li>
        </ul>
    </div>
    <div id="div1" style="width:17%; height:28%;" ><img src="img/107.png" />
    <form method="post" action="step3.php">
        <input name="color" type="text" value="" id="hiddencolor" />
        <input name="submit" type="submit" value="Submit" />
    </form>   
</div>

Answer №1

Adjust the size of the main container div from 17% to match the width of the img, specifically 250px

Example: http://jsfiddle.net/V9zEH/5/

Answer №2

        $('#div1 img').removeClass().addClass(newColor);

Is this the solution you were looking for?

If your goal is to prevent the color from bleeding outside the borders of the image, one workaround could be to modify the image itself by adding a white background where you don't want the color to show - rather than using transparency.

I'm having difficulty grasping the specific outcome you are aiming for. :|

Answer №3

Give it a shot:

$(document).ready(function () {
    $('.iconWrapper span').click(function (e) {
        var $span = $(this),
            color = $span.attr('class');
        $('#div1 img').css('background-color', $span.css('background-color'));

        $('#hiddencolor').val(color);
        e.preventDefault();
    });
});

See the outcome in action »

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

The NPM installation failure led to a crash

Every time I try to install a new npm package using 'npm install', my npm crashes. Sometimes, I have to repeat the process multiple times before all dependencies are installed properly, and even then, it might miss some nested dependencies. This ...

Cast the variable to access nested data

I'm currently working with data retrieved from an API call in the form of nested objects. My goal is to extract and organize this information for display in a table. In my code, I am using a loop with for (const [key, value] of Object.entries(result)) ...

Exploring the dynamism of ThreeJS with Sphere and Line movement

Update: Please Help! Looking for assistance with animating lines and spheres in a mesh simultaneously. I've created a mesh with lines and spheres, and now I want to animate them all together to create a smooth, pulsating movement. However, in my cu ...

Sending an HTTP POST request from Angular 4 to Java SpringMVC results in the issue of POST parameters not

Currently, I am developing a REST API using Java Maven Spring Boot and Spring MVC. I have encountered an issue where Angular POST request parameters are not being recognized by SpringMVC's @RequestParam. Below is the Angular code snippet; saveAsSit ...

Booking form pop-up appearing beneath the section

Currently, I am in the process of adding a booking form to the main page of . However, I seem to be encountering an issue where a popup appears below the section it is supposed to open within when selecting the number of persons. Despite adjusting the z ...

Experience a seamless front/back DIV transition with a mouseover effect similar to the ones on USAT

Recently, I was tasked with creating a mouseover transition effect for a div similar to the one used on USAToday's website. The structure of the boxes on the site includes: <div class="asset"> <div class="front">'image and some t ...

Learn the steps to switch the language settings in select2

Hello, I am currently using select2 version 4.0.3. I am trying to change the default language from English to Spanish. I have checked the internationalization section in the documentation, but unfortunately, the method suggested did not work for me. I am ...

Issue with data retrieval (Uncaught (in promise) TypeError: Unable to access properties of undefined (reading 'map'))

Greetings! Upon executing this code, I encountered an error related to the map() method. Despite its apparent simplicity, the issue persists. var div_usuarios = document.querySelector('#usuarios'); var usuarios = []; fetch('https://jsonplac ...

Choosing the appropriate value for a link button within a gridview

I am encountering an issue with my gridview where I am attempting to select the value of a link button clicked on a specific row. The code I have provided below is causing errors due to improper selection of the link button in the gridview. Can someone ass ...

Having a large number of columns makes it difficult to adjust the column width

Is there a way to maintain column width when there are multiple columns present? When the user clicks the + to view additional data, the Product Type column ends up looking uneven due to width issues. I attempted to use style="width:300px;", but unfortuna ...

Including a Javascript library (jsencrypt) in an Angular 2 application

I have gone through countless tutorials on this particular issue, but unfortunately, I have not yet found a solution. Let me provide some context first. I am working on an Angular 2 application and I need to incorporate this JS library for encryption: http ...

Issue: It seems like there is an error with using functions as a React child. Uncertain about the exact location

This specific issue is one of the two errors I have come across in the same application referenced in my previous inquiry. The first error encountered is as follows: Warning: Functions are not valid as a React child. This may occur if you mistakenly return ...

What is a more efficient way to avoid duplicating code in javascript?

Is there a way to avoid repeating the same code for different feeds? I have 8 feeds that I would like to use and currently, I am just incrementing variable names and feed URLs. <script type="text/javascript> function showFeed(data, content ...

States following form submission in an HTTP request may vary

When attempting to submit a form and receive the results on the same page using AJAX, a JavaScript function is called onsubmit, initiating the AJAX call and including the following code: request=new XMLHttpRequest(); /* set up request here ...

Save a version identifier in already deployed Redux stores for production settings

I am seeking a way to introduce a new bundle into a live React/Redux application that caters to users who are already logged in with a different state tree. To achieve this, I intend to prompt the logged-in users to logout so that I can reset the state tre ...

How to perfectly center the Bootstrap Modal using CSS styling

Looking for a way to vertically align the Bootstrap Modal? I've attempted using CSS with no success. I tried this: .modal-dialog { display: inline-block; text-align: left; vertical-align: middle; } However, the modal still displays in t ...

Create a customized template using the resolve feature in the $stateProvider

Take a look at this code snippet: bank.config(function($stateProvider) { $stateProvider .state('main.bank', { url: '/', controller: 'BankCtrl', resolve: { money: function(bankResource) { ...

Assistance needed in loading the gallery "galleria" using the .load function

With an image gallery containing over 100 images, optimizing loading speed is key. I aim to divide the gallery into groups of 30 and implement navigation tabs "Gallery 1 2 3 4 5" so that users can easily navigate through the content. When a user clicks on ...

Is it true that one line arrow functions cannot include a semicolon without braces?

As someone who is new to React and ES6 syntax, I stumbled upon an interesting observation. When I wrote the following code snippet: return <input onChange={event => console.log(event.target.value);} />; I encountered a "Cannot find module" error ...

Organize the Div elements in the form of a message

Here is the code I am working with: https://jsfiddle.net/bdqgszv5/1/ This is the same code without jsfiddle: <section id="aussteller" class="row separated"> <div class="section-header text-center"> <h2& ...