My code is giving me a headache because it just refuses to cooperate

There is an issue with my HTML code for creating 5 sliders. The numbers are changing in the first slider, but not in the other sliders.

<body>
    <div class="box">
        <div class="slider">
            <input type="range" name="eng1" min="0" max="180" value="20">
        </div>
        <div class="value">180</div>
    </div>
</body>

<body>
    <div class="box2">
        <div class="slider2">
            <input type="range" name="eng2" min="0" max="180" value="30">
        </div>
        <div class="outcome">180</div>
    </div>
</body>

Answer №1

When utilizing querySelector, it will choose the first element it comes across.
As a result, both querySelectors will scan the document from the beginning, locate the initial input tag and item with the "value" class, interacting with that specific element. This implies that both will target and manipulate the same slider and div.

Incorporate:

<html>

<body>
  <div class="box">
    <div class="slider">
      <input type="range" name="eng1" min="0" max="180" value="20">
    </div>
    <div class="value">180</div>
  </div>

  <div class="box">
    <div class="slider">
      <input type="range" name="eng2" min="0" max="180" value="30">
    </div>
    <div class="value">180</div>
  </div>
</body>

<script>
  const slider = document.querySelectorAll("input");
  const value = document.querySelectorAll(".value");

  for (let i = 0; i < 2; i++) {
    value[i].textContent = slider[i].value;
    slider[i].oninput = function() {
      value[i].textContent = this.value;
    }
  }
</script>

</html>

Avoid inserting multiple script and body tags. I have renamed all to the same class and simply utilized a loop within the script.

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

Assigning arbitrary hidden form data from dropdown selection

Would like to assign Layers Value as one of the following: GFS Meteogram 3day std or WRF 20 Global Meteogram 3day Std depending on the option selected from the dropdown menu <div><select id="myselect" class="productViewerParameter" name=" ...

Using a variety of different font styles within a single sentence

I'm looking to add a title with text in one of my divs, but I want the title to be in 3 different font sizes. Any suggestions on how to achieve this without starting a new paragraph for each size change? Here's an example of what I'm trying ...

Customize the color of arrows within an ng-bootstrap carousel

I am currently working on a carousel using ng-bootstrap, and I need to change the color or image of the arrows from white to black because my background images are white and not visible. The issue is that I am having trouble accessing span, and I am unsure ...

Thymeleaf is responsible for fetching the static resources by referencing the REST URI

I am currently utilizing thymeleaf as my chosen template engine for a spring boot project. Here is the directory structure: src/main/ |- java | - UserAdministrationController.java |- resources | - static | - admin | - css ...

Display the output of an array in JavaScript within the same document

I am struggling to print the contents of a JavaScript array data: [ JSON.parse(genreCanvas.dataset.count).forEach(count => { `${count},\n`; }) ], to achieve the following result: data: [ 1, ...

Is there a "Read more" link to expand the content on the page?

I am struggling to figure out why my JavaScript code for toggling between showing more text and less text is not functioning correctly. My goal is for the user to click on the "Show More" button to reveal additional text, and then be able to click on "Show ...

JavaScript encoding and decoding challenges

Can anyone help me figure out what's wrong? I'm trying to encode and decode a simple input, but it just doesn't seem to work! Any ideas why? Thanks in advance for your assistance :) ENCODE: function encryption_encode(s, delta) { var te ...

The Bootstrap row has surpassed the maximum height of the container-fluid

I've encountered a problem while working with the bootstrap grid system (Version v4.5.2). I have set a max-height of 50vh on a container-fluid, which is working fine. However, when I add a row with 2 columns to the container, the images within the row ...

Update JSON data in ng-blur event using AngularJS

Could you offer some guidance on how to push the content from a text box into JSON when I click outside of the box? Below is the code for my text box: <input type="text" name="treatmentCost" class="form-control" ng-model="addTemplate" /> And here i ...

What is the best way to showcase a full-screen overlay directly inside an iFrame?

As I work in HTML, I'm faced with a challenge - my iFrame is only taking up a small section of the screen (referred to as 'World' in the example below). What I want to achieve is creating a full-screen div overlay from that iFrame, but curr ...

JavaScript Summation Calculation

I am currently working on calculating the sum of three scores and displaying the total as "Total:". However, I am facing an issue in dynamically updating the total whenever a score value changes. Is there a way to utilize the "onchange" event to achieve th ...

Loading elements of a webpage in a consecutive order

I am currently working on implementing a content slider within my Django application. The specific slider that I am using can be found at this link. One challenge that I am facing is the need to load close to one hundred 'contentDiv' pages, and I ...

Utilize PHP to Extract ID3 Tags from MP3 Files and Embed Them into HTML

While conducting some research, I stumbled upon a helpful resource: ID3 Functions. This guide is all about PHP and how to utilize ID3 functions in web development. I'm interested in learning more about how to incorporate this into a website so that: ...

Getting rid of the IE pseudo-element that shows up on the right edge of a password textbox

My form consists of two text boxes for username and password entry. While entering text in these boxes, I noticed that Internet Explorer (IE) has specific pseudo-elements for the Username and Password fields: Username textbox: <input class="form- ...

Having trouble interacting with an element using Selenium in Python

I am trying to figure out how to download an image from Instagram, but I cannot seem to programmatically click on the download button of this page. Even after attempting to use XPath and CSS selector methods for clicking, unfortunately, neither seems to w ...

Angular causes HTML Dropdown to vanish once a null value is assigned

In my scenario, I have two variables named power and mainPower. Both of these variables essentially represent the same concept, with mainPower storing an ID of type Long in the backend, while power contains all attributes of this data transfer object. The ...

Ensuring radio button validation prior to redirecting to contact form

I created a survey form using HTML and CSS. I am looking to add validation to the questions before redirecting to the Contact form page. Currently, when a user clicks on the submit button in the Survey form, the page redirects to the contact form. Howeve ...

Internal link formatting using CSS styling

I have a question about formatting links with fonts and colors to make them clickable. I managed to achieve the desired font and color styles using a specific code snippet, but struggled to make the link clickable: <table border="0" cellpadding="1" cel ...

What steps should I take to address the original tag error for Vue.js in the index.html file?

Desired Learning Interested in delving into the Vue.js framework using Vim and this helpful guide: https://cr-vue.mio3io.com/guide/chapter1.html#繰り返しの描画 Encountered Issue Difficulty in getting Vue tags to be recognized and functional i ...

html occasionally fails to render in emails

Recently, I created a Java code to send HTML emails. The code works perfectly when sending the emails to multiple recipients at once. Below is the sample code: String content="<html><p><h1>This is my first Java email with an image but wi ...