Obtaining the data value from a style applied to a div element in an E-commerce platform like MyShop

I have recently started using MyShop for my online store at www.myshop.com. The programming language they use is quite different from what I am used to.

For example, the total price in the basket.html script is displayed using the following code:

<span style="myshop-value:price-total-formatted;white-space:nowrap;"></span>

This span tag displays the total price of products in the basket without any data inside it. It seems to be linked to a CSS stylesheet, which is a new concept for me.

Now, I am trying to create a JavaScript function that displays a message based on the price. If the total price is less than €25, it should show "You'll have free shipping", and if it's over €25, it should display "You'll have to pay for shipping".

Here is the JavaScript code for this function:

<div id="price"></div>

<p id="shipping"></p>

<script>
    var price = document.getElementById("price").innerHTML;

    if (price < 24.99) {
        document.getElementById("shipping").innerHTML = "You'll have free shipping";
    }

    if (price > 25) {
        document.getElementById("shipping").innerHTML = "You'll have to pay for shipping";
    }
</script>

However, I am facing an issue where the JavaScript function does not work correctly when I include the span tag with the style attribute inside the price div. It seems like the div cannot access the data. This type of programming is new to me, and despite my efforts to find a solution online, I have not been successful. Any help or explanation would be appreciated.

Answer №1

Check out this demo - http://jsfiddle.net/7n8qkr1u/

let labelContent = $('#theLabel').html();
let value = parseInt(labelContent.match(/\d+/)[0]);

To access the span element in JavaScript, I assigned it an id attribute. Once we extract the inner HTML, we'll remove any currency symbols and convert the resulting string into an integer value.

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

Oops! Property 'month' cannot be set on undefined value due to a TypeError

Despite not receiving any errors from Visual Studio Code, I’m encountering an error in Chrome's console. Below is the code snippet from my interfaces.ts file: export interface Data1{ month: string; employeeName: string; date: string; employmentSta ...

Pressing the shortcut key will activate the function specified in ng-click,

I have been searching for a solution to my problem, but I haven't found anything that really helps. What I am looking for is a shortcut within an ng-click directive where there is only an if condition without an else expression. Essentially, I just wa ...

error is not defined in the onsuccess function of the ajax.beginform partial view

Currently, I am working on an MVC5 project where a View is calling a Partial View. Within the Partial View, there is an Ajax.BeginForm that triggers a function on OnSuccess. However, during execution, I encounter an error stating that the function cannot ...

Utilizing jQuery with live content to determine dimensions as required

Within my web application, I have a page that dynamically retrieves a view with both HTML and JavaScript. The JavaScript is responsible for rendering a chart into the retrieved view. The problem arises because the chart library I am utilizing (flot) necess ...

Using three.js to set the HTML background color as clearColor

How can I set the HTML background as clearColor using three.js? Here is the code snippet for three.js: // Setting up the environment var vWebGL = new WEBGL(); var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / ...

Discover the following input

Can someone assist me? I am looking for a way to automatically move the cursor to the next input field after filling out the first one. Here is the code snippet I have been working with: jQuery: $('input').keyup(function(){ if($(this).val().m ...

Angular’s ng-repeat function is behaving erratically as objects are displayed in the console

Having trouble with my ng-repeat not displaying content. Here is the code for my app.js, client controller, factory, and server controller. Everything else seems to be working fine. I can see all the console logs in Chrome and terminal, but the ng-repeat ...

JQuery is unable to validate a form that is being loaded from an external HTML file

I'm encountering an issue where my form imported into a separate HTML file is not validating using JQuery's $(form).validate() plugin. After following the guidance provided in this question, I successfully managed to get the form working. Howeve ...

The html5 lightbox does not function properly when clicked on an ajax-loaded <a> link

I am looking to showcase the data from a text file within an HTML5 Lightbox. The HTML5 Lightbox functions properly when simply using it in any link on the page. For example: <a data-height="500" data-width="800" href="hello.txt" class="html5lightbox" ...

Using the Tailwind CSS framework in combination with Vue's v-html

My vue component is designed to accept a prop of raw HTML, which originates from a wysiwyg editor utilizing tailwind classes for styling - similar to our vue app. The issue arises when using v-html="responseFromAPI" in my component, as the raw H ...

PHP Ajax Tutorial - Sending an HTTP request and displaying the retrieved data

I currently have two files. One is an API called "sqltest.php" When I access it on my server, I receive the following output: ["39","blah"] My client.php file contains jQuery ajax code to retrieve this data and display it correctly in the web browser. ...

What is the best way to display an error message when the JSON result is unsuccessful?

When using Ajax, the success part always executes. However, if an error occurs, I want to display an error message instead of running the normal process. If a discount code is present, I need to show/hide certain div elements along with the code and then r ...

Having trouble with PHP AJAX POST and receiving incorrect data

My AJAX call to a PHP script is not returning the expected data. This is the AJAX request I am making: $.ajax({ type: "POST", url: 'http://www.boomlings.com/database/getGJUserInfo20.php', contentType: 'application/x-www-fo ...

How to stop Mouseenter event from bubbling up in an unordered list of hyperlinks using Vue 3

I've experimented with various methods to prevent event bubbling on the mouseenter event, but I'm still encountering an issue. When I hover over a link, the event is triggered for all the links as if they were all being hovered over simultaneousl ...

What is the process of running PHP in a .ctp file within the CakePHP framework?

I have recently started working with CakePHP and I have a question about how PHP code is executed in a file with the .ctp extension. Can you explain how PHP is processed within a .ctp file in CakePHP? Additionally, I'm also curious about executing PHP ...

Tips for identifying the category of a hyperlink within my javascript code?

Hello, I am currently working on a script that involves AJAX functionality: function ajax(){ if (navigator.standalone) return; for (var i= document.links.length; i-->0;) { document.links[i].onclick= function() { var req= new ...

Using Vue.js to dynamically update values in JavaScript code or tags

I've encountered an issue that I'm having trouble articulating, but I'll do my best to explain. Upon loading the page, the following code snippet, which uses jinja-based placeholders, is executed: <div class="ui container"> ...

Angular UI Bootstrap collapse directive fails to trigger expandDone() function

I am currently utilizing UI Bootstrap for Angular in one of my projects, and I have developed a directive that encapsulates the collapse functionality from UI Bootstrap. Here is how it looks: app.directive( 'arSection', ['$timeout', fu ...

Issue with Jquery Forms Plugin jqXHR response in Internet Explorer versions 8 and 9

I encountered a peculiar problem that only seems to occur in IE8 and 9, despite testing on Safari, Chrome (on Mac), Firefox (on PC), as well as IE versions 10 and above. Below is the code snippet causing trouble: $('#fileStore_newUpload').ajaxF ...

What is the predefined value for a multi-select generated by the ng-for directive in Angular?

I am having trouble setting default selected values for the multi-select. Despite trying various methods such as initializing the ngModel to bind the variable and using [selected] = "selectedSegment == 'S1'", none of them seem to be effective fo ...