Reputation: 45
HTML:
<span class="point">8,0</span>
<span class="point">8,0</span>
<span class="point">10,0</span>
<span class="point">8,0</span>
I want when if point is 10,0
, remove ,0
.:
<span class="point">10</span>
I was put alert with length but its not working.
$('.point').each(function () {
if ($('.point').text().length > 4) {
alert("ok");
}
});
It was all points get alert.
What's my problem? How can I solve it?
$('.point').each(function () {
if ($('.point').text().length > 4) {
alert("ok");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="point">8,0</span>
<span class="point">8,0</span>
<span class="point">10,0</span>
<span class="point">8,0</span>
Upvotes: 0
Views: 49
Reputation: 1114
It depends if just for 10 then...
$('.point').each(function () {
var textAfterComma = $(this).text().split(",");
if (textAfterComma[0] === '10' && textAfterComma[1] === '0') {
$(this).text(textAfterComma[0]);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="point">8,0</span>
<span class="point">8,0</span>
<span class="point">10,0</span>
<span class="point">8,0</span>
Working JSFiddle: http://jsfiddle.net/bc6jmtt2/1/
if you want all numbers ending with ,0 to be just the first number then...
$('.point').each(function () {
var textAfterComma = $(this).text().split(",");
if (textAfterComma[1] === '0') {
$(this).text(textAfterComma[0]);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="point">8,0</span>
<span class="point">8,0</span>
<span class="point">10,0</span>
<span class="point">8,0</span>
Working JSFiddle: http://jsfiddle.net/bc6jmtt2/
For you example at the bottom of your question there are two fixes:
First $('.point')
to $(this)
then if ($(this).text().length > 4) {
to if ($(this).text().length >= 4) {
or > 3
because none of your strings are > 4
$('.point').each(function () {
if ($(this).text().length >= 4) {
alert("ok");
}
});
Working JSFiddle: http://jsfiddle.net/bc6jmtt2/3/
Upvotes: 1
Reputation: 622
Use $(this).text()
instead. Using $(".point").text()
results in concatenation of all text of point class like: 8,0 8,0 10,0 8,0
So:
$('.point').each(function () {
if ($(this).text().length > 3) {
alert("OK");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="point">8,0</span>
<span class="point">8,0</span>
<span class="point">10,0</span>
<span class="point">8,0</span>
Upvotes: 0
Reputation: 10384
Because $('.point').text()
is the concatenation of the text in all elements with class .point
, you should use $(this).text()
.
Also, you condition should be >=4
or ==== "10,0
$('.point:contains("10,0")').text('10');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="point">8,0</span>
<span class="point">8,0</span>
<span class="point">10,0</span>
<span class="point">8,0</span>
Upvotes: 1