Retrieve and store all CSS classes within an array before performing a calculation

I am trying to input an exchange rate into a text field and update multiple text areas with new rates.

Exchange Rate <input type="text" value="" id="exchange_rate">
<input type="button" value="Update Price" id="update_price">

The number of text areas may vary, here are some samples:

<input class="price" name="pro_name[88197][1]" rel="4450000" value=""  type="text">
<input class="price" name="pro_name[12316][2]"  rel="4451000" value=""  type="text">
<input class="price" name="pro_name[46511][3]" rel="4575120" value=""  type="text">
<input class="price" name="pro_name[45151][4]" rel="2343400" value=""  type="text">
<input class="price" name="pro_name[165652][5]" rel="4534500" value=""  type="text">
<input class="price" name="pro_name[85559][6]" rel="4450000" value=""  type="text">

Now I want to calculate the value for each text area by multiplying the 'rel' (Japan YEN amount) with the exchange rate (USD).

I have started my jQuery code as shown below but I am facing some issues. Can someone please help me solve this problem?

$("#update_price").click(function () {
   var all_price_element = [];
   price_class = $( ".price" ).toArray();

   for (var i = 0; i < price_class.length; i++) {
     all_price_element.push(price_class[i].innerHTML);
   }
});

Answer №1

If you want to iterate through each element, you can use the .each() function:

Here is an example of how to do it:

$("#update_price").click(function (e) {
  //
  // get the current exchange_rate and convert it to a number
  //
  var er = +document.getElementById('exchange_rate').value;

  
  //
  // for each price element, perform the calculation:
  //
  // get the current rel value, convert it to a number and
  // multiply by rel
  //
  $( ".price").each(function(index, element) {
    element.value = er * +element.getAttribute('rel');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  Exchange Rate <input type="text" value="" id="exchange_rate">
<input type="button" value="Update Price" id="update_price">

<input class="price" name="pro_name[88197][1]" rel="4450000" value=""  type="text">
<input class="price" name="pro_name[12316][2]"  rel="4451000" value=""  type="text">
<input class="price" name="pro_name[46511][3]" rel="4575120" value=""  type="text">
<input class="price" name="pro_name[45151][4]" rel="2343400" value=""  type="text">
<input class="price" name="pro_name[165652][5]" rel="4534500" value=""  type="text">
<input class="price" name="pro_name[85559][6]" rel="4450000" value=""  type="text">
</form>

Answer №2

To update all prices, you can use the following method.

$("#update_price").click(function () {  
    var ex_rate = $('#exchange_rate').val();   
  
    $(".price").each(function() {
        var rel = $(this).attr('rel')
        $(this).val(ex_rate * rel);
    });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="price" name="pro_name[88197][1]" rel="4450000" value=""  type="text">
<input class="price" name="pro_name[12316][2]"  rel="4451000" value=""  type="text">
<input class="price" name="pro_name[46511][3]" rel="4575120" value=""  type="text">
<input class="price" name="pro_name[45151][4]" rel="2343400" value=""  type="text">
<input class="price" name="pro_name[165652][5]" rel="4534500" value=""  type="text">
<input class="price" name="pro_name[85559][6]" rel="4450000" value=""  type="text">

Exchange Rate
<input type="text" value="" id="exchange_rate">
<input type="button" value="Update Price" id="update_price">

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

evaluating differences in strings using internet explorer

Here is the code I have written to hide a list item (li tag) depending on its content: dom.find('ul#overlay_opt li').click(function(){ if($(this).attr('class') != 'deactive'){ var content = $(this).text(); ...

Are you prepared for changing DropDowns?

To ensure users make a selection from a specific dropdown menu, I have implemented the following code to trigger an alert if they fail to do so: <script> function checkSelection(){ var sel = document.getElementById(' ...

How come I am unable to scroll beyond my parallax section unless I move the mouse first?

Currently, I am working on a webpage that includes a Parallax landing image. In order for the Parallax effect to function properly, the image has a greater height, requiring users to scroll down. This leads to the appearance of a second scrollbar specifica ...

Stop Bootstrap Modal from Showing on Mobile Devices

I am struggling to find a solution to prevent the Bootstrap modal from appearing on mobile and tablet devices. Currently, the modal only shows up for IE9 and below as a way to alert users about limitations when using such outdated browsers. Despite attemp ...

A guide to adjusting the size of a mat-button using an svg mat-icon

I have PrimeNg and Angular Materials buttons on the same td. I am attempting to resize my mat-buttons to match the size of my pButtons but they are not adjusting properly. Should I consider using a different type of button with my icon? HTML <button ma ...

The issue arises when RadioBoxes do not submit properly after their `check` status is altered using JQuery

My form contains multiple radio boxes whose "checked" status I modify based on user interaction. Upon initial page load and submission without any changes, the POST request correctly includes the value of the checked radio box. However, when I use JQuery ...

How can jQuery iterate through each element's href attribute?

Although the title may be a bit confusing, I am confident that this task is achievable! I have a single button with the id of 'downloadAll' and multiple anchor tags with the class 'link' on my webpage. What I would like to accomplish, ...

Transforming numerical figures into written form within a specified range: A step-by-step guide

I have a unique custom range design that I have personally customized. Beneath each step of the range, there is a value displayed in a stylish green box. https://i.sstatic.net/vzeRK.png My query is regarding how I can replace the numeric values with tex ...

The functionality of .attachClickHandler() is not maintained when the page is refreshed or redirected

I've encountered an issue while trying to integrate a custom Google login button in my index.html file. Initially, everything functioned flawlessly upon page load, with console outputs 1, 2, and 3 appearing sequentially, followed by output 4 upon sign ...

Save essential data in local/session storage to have access to it even after the page is reloaded

Dear Team, we recently developed a single-page application that stores data in the root scope for access on other pages. While everything functions well in the regular flow, we encountered an issue with browser refresh. If a user refreshes the applicatio ...

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...

Creating a primary php file in Apache without the use of SQL or any database: is it possible?

Forgive me if this comes across as rude, but I'm struggling to grasp the concept of apache, PHP, and servers in general. To help myself understand better, I want to create a very basic website that assigns an ephemeral ID to each user (not a session). ...

retrieve all elements within a drag selection made by a mouse

Have you ever come across the image cropping tool with a selection option (marque tool) created in javascript? If not, check it out here: . I'm interested in finding a way to capture all elements within a selection like that. For instance, in Windows ...

What is the best way to position the hamburger menu icon on the right side of the header?

I need some assistance with adjusting the position of my mobile hamburger menu icon. It's currently on the left side of the screen, but I want to move it to the right. I've tried making the changes myself using CSS and HTML, but I'm not an e ...

Tips for ensuring that hover:after contents do not impact the positioning of the next element

I have created a photo list section on my webpage. <div id="main_post_photo"> <% for(var q=0;q<resolve.length;q++){ if(resolve[q].images.length>=1){%> <a href='/post/<%= resolve[q]._ ...

Tips for accessing values from an array in JavaScript

Is there a way to condense the id and name variables into a single variable? I attempted to use $('#vidyagames').tokenInput([variable]); and also tried inputting a URL, but nothing seemed to work. Instead of id and name, I have a function that re ...

Bootstrap 5's Vertical Ruler

Is it possible to add a vertical line that extends down the right side of the listed items on this website? I want to place the main content next to this vertical line. .first a.nav-link { background-color: #ebe0dd; } a.nav-link { font-size: 23px; ...

The back div is not retained on the second animation when flipping the card

In my card with a unique animation, clicking on the "Edit" button triggers the following actions: It smoothly transitions to the center of the screen. During this movement, it rotates by 180 degrees, revealing a blank green back content. Once the card r ...

"Unlocking the power of Bootstrap modals in Django

Using Django, I have a loop of div elements. When I click on a div, I want a modal to be displayed. Here is my HTML code: {% for object in theobjects %} <div class="row" style="margin-top:0.5%;"> <div name="traitement" <!-- onclic ...

Unable to assign an ID to an element in JavaScript, as it will constantly be undefined

Is there a way to automatically generate unique IDs for jQuery objects if they don't already have one? I need these IDs for future requests. I wrote the following code, but it doesn't seem to be working. The ID's are not getting set and the ...