Issue with image box-shadow not functioning as expected

Hey everyone, I'm struggling to understand why the onclick function on the image is not generating a drop shadow every time you click. Could someone please lend a hand? I am teaching myself how to program and any help would be greatly appreciated. (Please take a look at my Javascript file).

I'd like the final result to resemble this , but Jennifer wrote it in jQuery which I haven't learned yet.

MY CODE

    
    var newBlur = 0;
    var newSpread = 0;

document.getElementById("catIMG").addEventListener("click", shadowIMG);

function shadowIMG() {
    var myShadow = genShadow();
    document.getElementById("catIMG").style.boxShadow = myShadow;
    console.log(myShadow);
}

function genShadow() {
    var arr = [0, 0, newBlur, newSpread,];
    newBlur +=1;
    newSpread +=5;
    var newShadow = '"' + arr[0] + 'px ' + arr[1] + 'px ' + arr[2] + 'px ' + arr[3] + 'px ' + 'Black' + '"' ;
    return newShadow;
}
body
{
    margin: 0;
    background-color: lightgrey;
}

input {
    position: absolute;
    left: 10%;
    width: 100px;
    height: 50px;
    font-size: 20px;
    color: white;
    background-color: green;
}

#blueTop {
    font-size: 30px;
    font-family: Arial;
    width: 100%;
    height: 100px;
    background-color: mediumpurple;
}


#catIMG {
    position: absolute;
    left: 50%;
    top: 15%;
    transform: translate(-50%);
    width: 750px;
    height: 500px;
    cursor: pointer;

}

#catText {
    position: absolute;
    left: 50%;
    top: 800px;
    transform: translate(-50%);
    width: 750px;
    text-align: center;
    font-size: 50px;
    font-weight: 100;
    color: rgb(50,20,0);
    cursor: pointer;
}

h5 {
    position: absolute;
    left: 50%;
    top: 1000px;
    transform: translate(-50%);
    font-size: 25px;
    cursor: pointer;
}
<form>
<input id="testButton"type="button" value="Test"/>
</form>
<div id="blueTop"></div>
<img id="catIMG" src="./more_grumpy_shadow.png"/>
<h1 id="catText">"You will always be lucky if you know how to make friends with strange cats."</h1>
<h5>- Colonial proverb</h5>

Answer №1

The issue lies within this particular line:

var newShadow = '"' + arr[0] + 'px ' + arr[1] + 'px ' + arr[2] + 'px ' + arr[3] + 'px ' + 'Black' + '"';

You seem to be including unnecessary quotation marks.

// Here is some JavaScript code

var newBlur = 2;
var newSpread = 2;

// Functions section
document.getElementById("catIMG").addEventListener("click", shadowIMG);

function shadowIMG() {
    var myShadow = genShadow();
    document.getElementById("catIMG").style.boxShadow = myShadow;
    debugger;
    console.log(myShadow);
}

function genShadow() {
    var arr = [10, 10, 10, 10];
    newBlur += 1;
    newSpread += 5;
    var newShadow = '' + arr[0] + 'px ' + arr[1] + 'px ' + arr[2] + 'px ' + arr[3] + 'px ' + ' black' + '';
    console.log(newShadow);
    return newShadow;
}

body {
    margin: 0;
    background-color: lightgrey;
}

input {
    position: absolute;
    left: 10%;
    width: 100px;
    height: 50px;
    font-size: 20px;
    color: white;
    background-color: green;
}
/* CSS for blueTop element */
#blueTop {
    font-size: 30px;
    font-family: Arial;
    width: 100%;
    height: 100px;
    background-color: mediumpurple;
}
/* CSS for catIMG element */
#catIMG {
    position: absolute;
    left: 50%;
    top: 15%;
    transform: translate(-50%);
    width: 750px;
    height: 500px;
    cursor: pointer;
}
/* CSS for catText element */
#catText {
    position: absolute;
    left: 50%;
    top: 800px;
    transform: translate(-50%);
    width: 750px;
    text-align: center;
    font-size: 50px;
    font-weight: 100;
    color: rgb(50, 20, 0);
    cursor: pointer;
}
/* Additional styling for h5 element */
h5 {
    position: absolute;
    left: 50%;
    top: 1000px;
    transform: translate(-50%);
    font-size: 25px;
    cursor: pointer;
}
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <form>
            <input id="testButton" type="button" value="Test"/>
        </form>
        <div id="blueTop"></div>
            <img id="catIMG" src=""/>
            <h1 id="catText">"You will always be lucky if you know how to make friends with strange cats."</h1>
            <h5>- Colonial proverb</h5>
    </body>
</html>

Answer №2

Don't overcomplicate things with unnecessary work. Use JavaScript to easily adjust the blur and spread radius of an element when a user clicks a button.

var x = 0;
var y = 3;
document.getElementById("adjustShadow").addEventListener("click", function() {
    x++;
    y = x * 3;
   document.getElementById("dogIMG").style.boxShadow  = 
   "rgba(0, 1, 0, 0.35) 0px 0px "+x+"px "+y+"px";
});
body
{
    margin: 0;
   padding:20px;
}
 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link href="./adjustShadow.css" type="text/css" rel="stylesheet"/>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        
            <img id="dogIMG" src="http://via.placeholder.com/350x150"/>
      
<button id="adjustShadow">Adjust Shadow</button>

    </body>
    </html>

Answer №3

Resolved the problem by deleting the quotation marks from the newShadow variable. Appreciation to everyone who assisted me.

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

What is the best way to apply the 'active' class to a selected navigation link using JavaScript?

How can I use JavaScript to change the active click of a Bootstrap navbar based on which nav link was clicked? The issue is that the changes made by JavaScript are lost when the browser navigates to the corresponding page. MY CODE: document.querySelect ...

NextJs manages the logic for processing requests both before and after they are handled

NextJs stands out from other frameworks due to its lack of support for filter chains, request pre-processing, and post-processing. Many node project developers or library creators may find these features essential for executing logic before and after API c ...

How can I display or hide a specific div when it is clicked?

I have a list of icons that look like this: <ul> <li [ngClass]="iconM ? 'active': 'notactive'" title="Kontakti"><span (click)="showHide = !showHide"><i class="fa fa-user"></i><i class="fa fa-angl ...

Ways to choose tags that do not contain a particular tag within them

I am trying to utilize querySelectorAll to select all i elements that do not contain a font tag. The selector I attempted is: document.querySelectorAll('i > *:not(font)') <i> <font color="008000">Here goes some text</fon ...

What is the most effective way to transmit a conditional operator via a TypeScript boolean field?

Currently, as part of my transition to typescript, I am working on incorporating a conditional operator into the table component provided by Ant Design. const paginationLogic = props.data.length <= 10 ? false : true return ( <> ...

Error encountered when attempting to access a dropdown menu on a website due to a NoSuchElementException

In my current project using Python with selenium, I am encountering an issue while trying to interact with a list box on a webpage. The error message that I receive is as follows: selenium.common.exceptions.NoSuchElementException: Message: Unable to loca ...

Utilizing multiple criteria to filter through a nested array

I am faced with an array structured as follows const treeObj = [ { id: 1, name: 'Section One', items: [ { id: 1, name: 'Section One Item One' }, { id: 2, name: 'Section One Item Two' }, ...

Ensuring the same height for the navigation bar and logo with aligned vertical text

I'm in the process of recreating a simple navigation menu and I've reached the section where I am encountering an issue with the list items. The orange li element is not filling up 100% of the width, and I also need the links to align in the midd ...

Adjust the object size to match the Viewport dimensions

I've been developing a VR world where the camera can be turned and tilted in the center but not moved around freely. Within this virtual reality environment, you have the ability to attach a video to the background. Users can either play it by clicki ...

Unable to save or create files in Store.js

Recently, I've been trying to save a file on client storage using Store.js. After changing the date with store.set and logging it to the console successfully, I encountered an issue where the data was not being saved in the app directory as expected. ...

I am experiencing an issue with my Angular directive where the button click does not

Check out this page for guidance on adding a div with a button click in AngularJS: How to add div with a button click in angularJs I attempted to create an angular directive that should add a div to my HTML page when a button is clicked. However, upon cli ...

The error message "indexOf of undefined" appears when trying to read a property that does not exist within a new

Help Needed: The following error is happening: Cannot read property 'indexOf' of undefined at new HttpRequest (http.js:653) at HttpClient.request (http.js:1069) at HttpClient.get (http.js:1157) This occurs when I use the get() method from Ht ...

What separates the onclick functionality of buttons from that of divs?

Initially, this issue may be specific to Firefox, though it is uncertain. Let's delve into some code: <!doctype html> <html> <head> <meta charset="UTF-8> <title>Text Editor</title> <!-- Custom CSS -- ...

Broken styles when using Material UI with babel and the 'with styles' feature

We've implemented babel and lerna to maintain specific elements of our react web app separately. While the setup has been effective thus far, we're encountering styling issues when generating the static build. The main project is not processed t ...

What steps can be taken to create a two-column layout using the provided HTML code?

I'm having trouble getting my CSS to work properly in IE 8. Any suggestions on how to fix this? Here's the simplified HTML code I'm using: <div id="column-content"> <div id="content"> <p>This is some text</ ...

Using the jQuery unbind method allows the function to execute only once, however, the event will not be

On my main page, I have a radio button that passes a value via Ajax when clicked. The result is then checked by Ajax and the corresponding output is displayed for each question as correct/incorrect. I am facing an issue with the unbind event - it works fin ...

What is the best way to utilize an Angular service method from a controller?

As a newcomer to Angular, I have created an 'Employee Search' Service module. Let's take a look at the code: // Employee Search Service app.service('employeeSearchService', function($http, resourceServerAddress){ this.empList ...

What is the best way to update the state of a particular slider component?

When trying to update the value of a specific slider during the onChange event, I noticed that it was affecting all sliders instead. Is there a way to target and set the state of only one slider during this event? Here's what I've attempted so f ...

Certain javascript files have had illegal characters mistakenly added to them

There seems to be an issue with the js files or server on certain pages, as they are not loading in Firefox: with firefox: SyntaxError: illegal character 癡爠浥湵楤猠㵛≶敲瑩捡汭敮產崻 ⽅湴敲⁩搨猩映啌敮畳Ⱐ獥灡牡瑥 ...

Encountering a JavaScript heap out of memory error during the upgrade from Angular v11 to v12

After running my filesystem indexing script to refresh RAID files index for 4 hours, it unexpectedly crashed with an error. View error image here The server has 8GB of RAM. During the upgrade from Angular v11 to v12, I encountered an error. Everything w ...