0

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?


Snippet

$('.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>

2
  • 3
    if ($('.point').text().length > 4) { should be if ($(this).text().length > 4) { Commented Nov 20, 2014 at 18:39
  • Also >= 4 or > 3 Commented Nov 20, 2014 at 18:58

3 Answers 3

1

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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

Here's a better way:

$('.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>

Comments

0

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>

3 Comments

Why even post an answer that just mimics the other answers?
Sorry, I started typing it before the other one was posted and only realized after submitting my answer. Should I delete it then? (I joined just recently and hence not very familiar with all the workings)
No, just 15 minutes after our answers you posted. Seemed like you were just trying to copy an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.