Guide to choosing a class::-webkit- using JavaScript or jQuery

I am facing difficulty in selecting a CSS class named .slider::-webkit-slider-thumb to modify a property. Despite trying various options, JavaScript doesn't seem to be targeting it.

document.addEventListener("DOMContentLoaded", function (event) {
 var slider = document.getElementById("importanceSlider");
 var knob = document.getElementsByClassName(".slider::-webkit-slider-thumb");

 knob.style.background = "#ffffff"; });

CSS:

.slidercontainer{
    width: 100%;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 25px; 
background: #e9ecef;
outline: none;
border-radius: 50px;
}

.slider::-webkit-slider-thumb {
-webkit-appearance: none;
width: 10%;
height: 25px; 
background: #dc3545;
border-radius: 50px;
}

HTML:

<div class="slidecontainer">
     <input type="range" min="0" max="10" value="5" 
    class="slider" id="importanceSlider" name="importance">
</div>

I am looking to dynamically change the width value of .slider::-webkit-slider-thumb based on the slider input.

Your assistance with this matter would be highly appreciated.

Answer №1

It's important to note that the pseudo-selector used here is not standard...

This feature is non-standard and is not on a standards track. Avoid using it on live sites: it may not function for all users and could have compatibility issues between browsers with potential behavior changes in the future.

Exercise caution when implementing it.

If you are working within a compatible browser, there is another issue:

There appears to be confusion between querySelector and getElementsByClassName. The former allows selection based on CSS selectors while the latter does not. Additionally, the latter returns a list of nodes.

Consider utilizing a stylesheet rather than directly naming your class like this.

Below are a couple of suggested solutions:

1) querySelector

var knob = document.querySelector(".slider");
knob.classList.add('blue');
.slider::-webkit-slider-thumb {}
.blue { background-color: blue; }
<button class="slider">Carrot</button>

2) getElementsByClassName

var knob = document.getElementsByClassName("slider");

// Select the first element from the list
knob[0].classList.add('blue');
.slider::-webkit-slider-thumb {}
.blue { background-color: blue; }
<button class="slider">Carrot</button>

If you have multiple elements, you will need to iterate through them. This can be done with getElementsByClassName, or by using querySelectorAll:

var knobs = document.querySelectorAll(".slider");
[...knobs].forEach(knob => knob.classList.add('blue'));
.slider::-webkit-slider-thumb {}
.blue { background-color: blue; }
<button class="slider">Carrot</button>
<button class="slider">Spam</button>
<button class="slider">Goat</button>

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 could be causing NPM to generate an HTTP Error 400 when trying to publish a package?

My current goal is to release an NPM package named 2680. At the moment, there is no existing package, user, or organization with this specific name. Upon inspection of my package.json, it appears that everything else is in order. Here are all the relevant ...

Is there a way to update my Google Maps .html file on my android phone to pan to my current location?

An objective is to create a local .html file on an android device that can be accessed through a standard web browser, allowing users to pan to their current location on Google Maps. Presently, the panning feature works on the Mi browser on android devices ...

Build an intricate nested array structure using the properties of an object

My data object is structured like this: "parameters": { "firstName": "Alexa", "lastName": "Simpson", "city": "London" } The task at hand involves implementing the followin ...

Is there a way to track and observe the vertical movement of an element within a directive's link function?

Can you help me figure out the best way to $watch or bind the height position of an element? I have a scenario where I display three divs, one at a time in different tabs (using bootstrap). Each div has a different height, and my directive comes after thes ...

Optimal Strategies for Handling CSRF Tokens with AJAX Requests in Laravel 9 and Beyond

When working with Laravel 9+, it is crucial to expose CSRF tokens for AJAX requests in order to maintain security measures. However, the placement of these tokens can impact code organization and elegance. There are two main approaches: Approach 1: Direct ...

Distinguishing between setting a value in the state directly versus setting a value within an object in the

I have noticed a difference in behavior based on whether I am using a boolean value with useState directly, or if I am using a boolean value inside an object with useState. The following code snippet demonstrates how the hidden text will be displayed when ...

What is preventing the jQuery GET function from saving the dynamically sortable UI list?

I'm having trouble saving the order of my jquery ui sortable list to the server. When a user clicks a tab, I load the list from an external file and then add a checkbox to each item. The issue arises when I insert the line to prepend the checkbox - su ...

Can you group polygons in layers using Phaser?

My goal is to animate two polygons: one that remains stationary and another that changes shape while moving. The challenge is to ensure that the stationary polygon always stays on top when overlapping. This is my proposed solution: (function() { "use ...

When attempting to call a bundle file using browserify from React, an unexpected character '�' Syntax error is thrown: react_app_testing/src/HashBundle.js: Unexpected character '�' (1:0

Hey there, I'm currently struggling with an unexpected unicode character issue. Let me provide some context: I've created a simple class called HashFunction.js that hashes a string: var crypto = require('crypto') module.exports=class H ...

Trouble detecting click event in jQuery after triggering radio button in HTML

Encountering a peculiar jQuery issue where triggering a click on a radio button does not fire completely and is ignored by an on click function, while a similar call to the jQuery trigger method is successfully captured. In the below jQuery snippet, a < ...

Tips for eliminating unnecessary data in written content

Could anyone provide a recommended method for removing unnecessary symbols from text strings? For instance, transforming "CWC%20-%20Maint%20Eng%20-%20El" into the more readable format of "CWC - Maint Eng - El". ...

Challenges with JSON Documents

const fs = require('fs'); const express = require('express'); const app = express(); app.use(express.json()); app.get('/submit', (req, res) => { let Com_Title = req.query.ComTitle; let Com_Text = req.query.ComTex ...

The duration of recorded audio in JavaScript is unclear

I managed to successfully create a structure for recording and downloading audio files. However, I'm facing an issue where the final downloaded file has an unknown duration. Is there any way to solve this problem?? Here is my Typescript code snippet: ...

After selecting "ok" on the JavaScript alert dialog, the webpage will automatically refresh, causing all information entered into the textboxes to be erased

<?php include "dbconfig.php"; session_start(); ?> <!DOCTYPE html> <html> <head> <title>Log in</title> <link rel="stylesheet" type="text/css" href="styles.css"> <link rel="stylesheet" type="text/css" href="bo ...

Segment the progress bar into sections

Struggling with an Angular app, and still new to HTML and CSS, I am attempting to create a unique progress bar. I aim to split each section of the progress bar into two subsections with distinct background colors shown in this image: https://i.sstatic.net/ ...

Duplicating an Angular 2 reactive form without retaining the original reference

I am facing a challenge with my reactive form where I need to round certain values when the form is submitted, without altering their appearance on the page. To achieve this, I devised a method that generates a new form, rounds the specified values, and t ...

Maximizing the Benefits: Unlocking Potential with the WD Calendar's Dropdown

I recently came across this amazing WD calendar project that I'm incorporating into the project I'm currently working on. You can find it at this link: . However, I am facing a small challenge in remaking the "Add New Event" form as I can't ...

Changing the li tag by clicking on it is a simple task that can be easily

Whenever I click on a tag, I want the li class to change to "active" and open a new page with the corresponding tag as active. For example, if I click on the Overview tag, the new page should open with the li tag as active. I have attempted to write some c ...

The website content is not visible on Internet Explorer version 10

Issue with Internet Explorer 10 I recently completed a new website for our company: . However, I am encountering some challenges with the site when viewed on IE 10 on Windows 7. For some reason, a large portion of the text is not displaying properly in IE ...

Showing pre-formatted HTML content that has been fetched from a JSON entity

I have an HTML content that is pre-formatted and stored in a JSON file. <p>This text contains <b>formatted</b> <i>content</i></p> Here is the format: { "description" : "<p>This text contains <b>formatted& ...