Is there a way to adjust the brightness value in a CSS filter using JavaScript without affecting the other filter attributes?

My goal is to implement CSS filter effects using JavaScript selectedItem.style.filter. However, when trying to apply the brightness attribute, it removes the contrast attribute.

Here is the code snippet:

$('.brightness').on('input', function(){
  var bright = $('.brightness').val();
  var selectedItem = document.getElementsByClassName('box')[0];
  selectedItem.style.filter = "brightness("+bright+"%)";
});

$('.contrast').on('input', function(){
  var contrast = $('.contrast').val();
  var selectedItem = document.getElementsByClassName('box')[0];
  selectedItem.style.filter = "contrast("+contrast+"%)";
});

If I adjust the code as follows, I can only update one attribute (either brightness or contrast) on input. How can I update only brightness without contrast?

selectedItem.style.filter = "brightness("+bright+"%) contrast("+contrast+"%)";

You can view a sample of the code in action on jsfiddle.

Please note: I am utilizing JavaScript code selectedItem.style.filter within ReactJS to modify the filter value, hence I require code in pure JavaScript, not jQuery.

Answer №1

Merging both values into a single event is more convenient. Assign the .slider class to both sliders.

$('.slider').on('input', function () {
    var bright = $('.brightness').val();
    var contrast = $('.contrast').val();

    var selectedItem = document.getElementsByClassName('box')[0];
    selectedItem.style.filter = "contrast("+contrast+"%) " + "brightness("+bright+"%)";
});

This method applies to adjusting both brightness and contrast simultaneously.

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

Tips for accessing all translation keys in next-i18next

Query Overview How do the majority of users incorporate the translation key into the json file during the translation process? By manual input...? ...

Tips for incorporating drop-down arrow states into a Bootstrap 4 accordion with Angular or CSS

Is there a simple way to implement an arrow (>) to show the aria-expanded="true" or aria-expanded="false" states in the Bootstrap 4 accordion using either Angular2 or plain CSS? I have gone through several tutorials and attempted numerous methods, but ...

Passing a variable from a child component to a parent component in Angular 2 using EventEmitter and a

I am experiencing a challenge with the Event Emitter in my child component. My goal is to pass a variable with EventEmitter to the parent component, but I would like to include a timeout function. Currently, the code looks like this: CHILD: export class ...

Can you explain the distinction between server-side rendering in Next.js and static site rendering in Gatsby.js?

I'm interested in developing a website without depending on client-side JavaScript, but I still want to incorporate SPA features such as client-side routing. To achieve this, I am considering using a framework that does not rely on rendering on the cl ...

Extract data from the Ajax database and automatically hide the "Load More" button when all items

Every time I fetch data from my MySQL database, I retrieve 5 items at once. $query = $pdo->prepare("SELECT * FROM names WHERE id < ? ORDER BY id DESC LIMIT 5"); $query->execute([$_POST["id"]]); while($row = $query -> fetch() ...

Mongoose: Unable to fetch item using its specific identification - Error 404

I've been attempting to fetch objects from MongoDB using Mongoose, but I keep encountering a 404 error. router.get('/blogs/:id', function(req, res){ console.log('trying to get one blog post by id'); Blog.findOne({ _id: req.para ...

Unique browsing key

Is there a specific identifier that can uniquely represent the browser you are currently using? I have two applications logged in through ApiGateWay, and I need to determine whether they are both running on the same browser. Therefore, I require a unique ...

Ideas for obtaining dynamic HTML content using Selenium?

I'm currently working on developing a web crawler in Python that can analyze the HTML of a website and search for all href tags. However, I've found that libraries like Beautiful Soup do not allow me to access the dynamic content generated by scr ...

Angular struggles to display carousels using templates

I've been working with Angular and UI Bootstrap components to create a carousel using the templateUrl argument for slide rendering. However, I've encountered some issues along the way. Firstly, nothing seems to appear on the page when I run it, a ...

Create a design where two columns can be scrolled independently and extend to fit the height of the viewport

I have successfully created a JS Fiddle that demonstrates exactly what I am looking for: http://jsfiddle.net/j7ay4qd8/5/ This Fiddle features two columns that can be scrolled separately, and different sections of the left column can be easily scrolled to ...

How can one efficiently obtain a "string write stream" according to common usage?

I may be viewing this through the lens of the JVM, but there is a third-party API I am utilizing that requires a WriteStream in its function declaration (specifically using the .write() method). Instead of writing to a file stream as commonly done, I want ...

Labels are overlapping each other and being aligned to the right side

Working with the most up-to-date version of Bootstrap, I am currently designing a search bar featuring a dropdown menu preceding the search button. Upon clicking the dropdown, various controls are revealed. Below is the HTML code for this configuration: ...

Challenge with CSS selectors

Here is the HTML code I am working with: <label for="tx_alterneteducationcatalog_subscriberadd[newSubscriber][gender]" class="error" id="tx_alterneteducationcatalog_subscriberadd[newSubscriber] [gender]-error">This field is required.</label> ...

How can I use a specific condition to query for a particular element in an $in array in MongoDB?

Although I am skeptical that this is achievable, I will give it a shot here. Below are some examples of records in the database: { type: 'fruit', name: 'apple', quantity: 3 } { type: 'fruit', name: ' ...

Ways to link my frontend to a NodeJS backend that is publicly deployed rather than on localhost

When my NodeJS server is running on localhost PORT, I can successfully connect them both by using the following code: const PORT = 9000; const app = express() app.listen(PORT, () => console.log(`Server is up and running on PORT ${PORT}`)) app.use(bodyPa ...

Error in NodeJS when trying to run ESM: "ReferenceError: exports is not defined

Having a tough time with this task. I'm attempting to execute ESM scripts on Node 14 (AWS Lambda) I am working on running this piece of code to convert 3D objects to THREE JSON. This involves using the command node -r esm fbx2three.js model.fbx. I ...

I'm curious if the response order will mirror the URL order in my situation

QUERY: Upon reviewing the following link: Promise.all: Order of resolved values I am doubtful about its relevance to my specific scenario. Can I always expect responses to be in the same order as urls? EXAMPLE: var urls = []; for (var i = 0; i < d ...

Is there a way to update a select box in JavaScript without refreshing the entire form?

Recently, I've encountered a challenge with using a form inside a modal. I need to dynamically add new fields to the select box and have them appear without reloading the entire page. Is this feasible? If so, could someone guide me on how to achieve t ...

Building an Empty AngularJS Response with the Combination of Spring Boot and MongoDB

In my AngularJS application, I collect user input and store it in painting objects. These objects are then sent to my Spring Boot backend which saves them to a MongoDB server and returns an ID. However, when attempting to POST data to the server, I receive ...

Preserve Tinymce's Raw Html in Laravel 4

Is there a way to retrieve the HTML source code saved in TinyMCE using AJAX in Laravel 4 without Laravel stripping all tags when getting input from the controller? For example, when sending post data through AJAX: var form = {id: 1, html: &apo ...