What is the best way to include fixed text before a value?

Hey there, I'm working on a project where the output value changes based on the selection made. I'd like to display the text "Total €" in front of the value like this:

.

However, the current output looks like this:

.

Additionally, I want to replace the "." in the value with a ",". But whenever I try to make adjustments, the blocks start overlapping. I'm hoping someone here has some solutions or ideas.

This is how I've styled the value output in my HTML:

<h1><input type="yeets" name="_mdjm_event_cost" id="yett" class="mdjm-input-currency required" readonly="readonly" value="" placeholder="Totaal €0.00" style="
  height: 100px;
  width: 150px;
  margin-top: -40px;
  color: black;
  font-weight: bold;"/>
</h1>

Here's the value selector I'm using:

<select name="iFenceCorners" id="FenceCorners" class="AutosubmitCalculator" tabindex="3">
  <option value="0" selected="selected" data-price="00.00">No corners</option>
  <option value="1" data-price="20.00">1 corner</option><option value="2" data-price="40.00">2 corners</option><option value="3" data-price="60.00">3 corners</option><option value="4" data-price="80.00">4 corners</option>
  <option value="5" data-price="100.00">5 corners</option><option value="6" data-price="120.00">6 corners</option><option value="7" data-price="140.00">7 corners</option><option value="8" data-price="160.00">8 corners</option>
  <option value="9" data-price="180.00">9 corners</option><option value="10" data-price="200.00">10 corners</option>
</select>

And here is the Jquery Script I'm using:

base = 0;
$("#FenceCorners").on("change", function () {
    var total = (base * 100 + $(this).find(":selected").data("price") * 100) / 100;
    $("#yett").val(total.toFixed(2).replace('.', ','));
});

Answer №1

initialValue = 0;
$("#FenceCorners").on("change", function () {
    var totalCost = (initialValue * 100 + $(this).find(":selected").data("price") * 100) / 100;
    $("#yett").val('Total amount: €' + totalCost.toFixed(2).replace(/\./g, ','));
});

For more information: https://www.w3schools.com/jsref/jsref_replace.asp

Answer №2

Check out this suggestion...

base = 0;
$("#FenceCorners").on("change", function () {
    var total = (base * 100 + $(this).find(":selected").data("price") * 100) / 100;
    total=total.toFixed(2);
    var suffix = "Total € ";

    var txt = suffix + total;

    $("#yett").val(txt);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="iFenceCorners" id="FenceCorners" class="AutosubmitCalculator" tabindex="3">
  <option value="0" selected="selected" data-price="00.00">No corners</option>
  <option value="1" data-price="20.00">1 corner</option><option value="2" data-price="40.00">2 corners</option><option value="3" data-price="60.00">3 corners</option><option value="4" data-price="80.00">4 corners</option>
  <option value="5" data-price="100.00">5 corners</option><option value="6" data-price="120.00">6 corners</option><option value="7" data-price="140.00">7 corners</option><option value="8" data-price="160.00">8 corners</option>
  <option value="9" data-price="180.00">9 corners</option><option value="10" data-price="200.00">10 corners</option>
</select>
<h1><input type="yeets" name="_mdjm_event_cost" id="yett" class="mdjm-input-currency required" readonly="readonly" value="" placeholder="Total €0.00" style="
  height: 100px;
  width: 150px;
  margin-top: -40px;
  color: black;
  font-weight: bold;"/>
</h1>

Answer №3

Imagine you have a total amount displayed within a div with the class "amount," you can easily include additional text using CSS.

<div class="amount">60,00</div>

.amount:before {
  content: "Total € ";
}

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

Bootstrap 4's container overflowing feature

I am trying to create a container with both horizontal and vertical overflow using the "auto" value. I want the content inside this container, which will mainly consist of large tables, to be scrollable within a window. It would be ideal if this container ...

Can JavaScript be used to retrieve the XML of an SVG image?

My objective is to incorporate an SVG image in JavaScript and have it accessible as both an image and the underlying SVG XML. To load the image, I can use the following code snippet... var myImage = new Image(); myImage.src = "cat.svg"; After obtaining ...

retrieving a date from a different source and displaying it in a date

Is there a way to ensure that the date format in an input of type date always follows the dd/MM/yyyy Brazilian pattern, regardless of the computer or browser specifications? <div class="input-group input-group text-center in ...

Show information - press the button on the current PHP page

After successfully reading files via PHP from a directory and dynamically creating a drop-down menu to view the file content, I have a query. Is there a way to display the text files' content on the same page without calling another PHP page? I know t ...

Performing a cross-domain JSON Get request with JQuery

Struggling with a simple JSON get request to an API on a domain that I don't have control over. Here's the code I'm using: $(document).ready(function () { $.ajax({ type: 'GET', url: 'http://pu ...

What could be causing the closest() method in my JavaScript code to not locate the nearest class?

I've encountered an issue while dynamically creating new classes with input fields in my project. For some reason, when I try to remove a previous row, nothing happens. Can anyone help me troubleshoot this problem? Here is the JavaScript code that I ...

Reorganize a list based on another list without generating a new list

Among the elements in my HTML list, there are items with text, input fields, and tables. I also have a specific order list like [3,1,2,0]. Is it feasible to rearrange the items in the HTML list on the page based on this order list without generating a new ...

Transform charset X to Unicode using Java

What is the process to convert a specific charset to unicode in Java? We have discussed charsets extensively, but this particular topic has not been covered yet. I have a hex string that follows the criteria length%4==0 (e.g. \ud3faef8e). Typically, ...

Dynamic scrolling text for overflowing text display

Here's a scenario I'm facing: Let's say I have three div tags, each 100 pixels wide: <--- DIV WIDTH ---> Text in div 1 Text in div two, it overflows Text in div three <--- DIV WIDTH ---> Currently, I've set the following C ...

The height of the parent div increases as its children grow when the browser is resized

I have a set of three divs aligned horizontally within a parent div. As the browser width changes, one of the child divs wraps under the other two when the width is reduced, transitioning them to a vertical alignment. Here are some visual examples: View i ...

Chrome experiencing issues with box-shadow on display:table-row-group

box-shadow doesn't seem to be functioning properly in Chrome for me. In my table layout, I need to use the display property set as table-row-group. I've been attempting to apply a box-shadow to the tbody element but it's not producing the d ...

What is the best way to add a value to the value attribute of a div using JavaScript?

Is there a way to insert a value into the value attribute of a div using Internet Explorer 9? I have attempted various JavaScript functions, but so far I can only modify the content inside the div and not the value attribute itself. <div id="mydiv" val ...

How can I vertically center a back button in jQuery Mobile?

Recently, I've been incorporating the back button into my jQuery Mobile page. One thing I'm trying to achieve is vertically aligning the back button in the header. Appreciate any help in advance! ...

Stay up-to-date with the latest headlines on your Android device!

I am currently developing an Android application and I need to retrieve the most recent news from this specific page, specifically the tab labeled "NYHEDER": Once I have retrieved the news, I plan to display it on the app's screen. I have come acros ...

Learn how to create a horizontally scrollable ToggleButton using MUI when the ToggleButtonGroup exceeds the screen width

Looking to enhance the responsiveness of my web design project, I aim to create a horizontally scrollable ToggleButton using Mui. The goal is to have the buttons adjust when the screen width becomes too narrow to display all three buttons at once, allowing ...

Use the $.get() method in JavaScript to send a parameter to my controller function

My objective is to showcase the event details in a modal. To achieve this, I am running a JavaScript script that calls the "GetEventsDetails" method in my "Event" controller with the event ID. While debugging in Chrome, I can see the ID being passed corre ...

JavaScript encountered a ReferenceError: 'a' has not been declared

One interesting feature of my slider is that it uses links like <a id="foo" class="oslide">Chineese Food</a> to navigate through the slides. To make this navigation function properly, I had to include a.href = "#"; in the link's click even ...

Securing my private key on a webpage from potential exposure by mandrillapp

After successfully creating and adding the key in Mandrill, I am able to send emails from my JavaScript page hosted at this link: However, I am facing an issue where my Mandrill key is publicly visible (in the contact_me.js file). I attempted to restrict ...

How can you toggle the visibility of a text based on the selection of a jQuery radio button?

Is it possible to dynamically enable or disable a field based on the selection of a radio button? Here are the rules: The field should only be active when the "Inactive" radio button is selected Check out the code snippet below for reference: Radio B ...

Creating image gallery with navigation arrows in JS/jQuery to loop through array of thumbnails

I am relatively new to working with arrays, and I have been exploring ways to use them in controlling the positions of thumbnails when navigating through a series of images using next or previous buttons. My goal is to make the thumbs loop seamlessly whene ...