1
I am in need of help with the following question:
In an HTML price list, I want to create a function using jQuery/Javascript so that as soon as the page loads, we select the HTML object, transform it into a string, split it after the comma, and then replace the HTML object with the variable
$(document).ready(function() {
var stringin = $('.priceList', 'ul', 'li').toString();
var formatedPrice = $(stringin).split(',');
$('.priceList', 'ul', 'li').innerHTML(formatedPrice[0]);
});
.price {
position: absolute;
top: 100px;
left: 100px;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Cents</title>
<link rel="stylesheet" href="main.css">
<script src="jquery-3.2.1.min.js"></script>
</head>
<body>
<div class="priceList">
<ul>
<li>R$ 50,89</li>
<li>R$ 188,43</li>
<li>R$ 1200,24</li>
</ul>
</div>
</body>
</html>
Above was my idea, the steps were: create two variables, the first to select the object and turn into a string, in the second I divide the string into two from the comma. Then, we take the first array "[0]" and change the HTML. While I am writing the question I have already stopped to think that in the case, as I am dealing with more than one value, it would be correct to make a vector, or I am mistaken?
In the meantime I’ll try to make a vector, anything I put here if it works.
Anyway, I need help, if there’s another way to accomplish the goal please let me know. maybe another logic, or with pure Javascript...
got a little confused your explanation you want to remove the comma from the pennies that?
– Lucas Azambuja
I also don’t understand what you want to do, but your jQuery is kind of crooked and weird.
innerHTML
in a jquery? objecthtml()
. And if I were to useinnerHTML
. should match this to the value, not between parentheses, aka:document.querySelector('.priceList', 'ul', 'li').innerHTML = formatedPrice[0]
I also don’t understand why you turned it into a jquery objectvar formatedPrice = $(stringin).split(',');
to use split, if split is made to be used in arrays, not jquery objects. You need to review your code guy.– Máttheus Spoo
opa, I apologize for the explanation, but the goal is, for example, a value: "R$50,99" I want to display only "R$50", without the pennies.
– MSHijo
Half crooked and not weird... completely crooked and weird, is that I’m still beginner bro, ended up mixing a lot of stuff still unfortunately... seriously? i thought that with the split I could transform a string into an array with two positions, like the idea is that [0] is the "R$50" and the [1] everything that is after the comma
– MSHijo
in case then, if I need to use innerHTML, it would look like this:
– MSHijo
innerHTML = formatedPrice[0];
– MSHijo
You can use
split()
in a string, the point is that you took the variable and threw it into a jquery object, placing the$()
around. That happens to be an object, not a string. And then thesplit
will not run properly.– Máttheus Spoo
is I messed up too much, the guy who helped down there, used . text()... but anyway, the whole code was wrong...
– MSHijo