Take data from the page by javascript and convert it to whole

Asked

Viewed 527 times

2

There is a problem, when picking a number within a span by javascript I try to convert it into int with parseint, but nothing. For if I try to make a sum with another number it ends up concatenated. and if I convert with parseint it appears 'NAN'.

HTML:

<div id="qualquer"><span>5</span></div>

Javascript com Jquery:

numeroRecuperado = parseInt($('#qualquer span').text().replace(/[\d]/g, ''));
numeroQualquer = 5;

total = numeroRecuperado + numeroQualquer;

$("#qualquer span").text(total);

As a result it appears NAN.

  • 1

    Add the code you already have to the question.

  • Don’t have any space in the string you’re trying to convert to integer? Maybe call the function. Trim() in variable containing span content resolves.

  • Possible duplicate of How I make a whole division?

  • Nothing to do, it’s not duplicated! Guys I put the code

  • @Romariopires give a console.log(numeroRecuperado) and tell us what was shown in console.

  • Your code is wrong. Your code takes out all the numbers and leaves only what is not number in the text

Show 1 more comment

3 answers

3

Follow the solution upon your code:

numeroRecuperado = parseInt($('#qualquer span').text().replace(/\D/g,''));
numeroQualquer = 5;

total = numeroRecuperado + numeroQualquer;

$("#qualquer span").text(total);

follow fiddle

  • I think you run the risk of having non-numeric characters in span.

  • @jbueno, but by itself the parseInt no longer does the job of clearing non-numerical?

  • @Wallacemaxters don’t take out hehehe' fit to test :D

  • 1

    @Wallacemaxters I tested on nodejs and he didn’t do it.

  • 1

    See the Suggestion which I gave in my reply. Using the data is better than taking the HTML value. Since we are using jQuery... that was not in the question tags...

  • 1

    @Wallacemaxters really gets more semantic and stylish :D

Show 1 more comment

2


I notice from your code that there is an error. You used the regular expression /[\d]/g. But this regular expression captures numerical values. That is, you are asking to replace numerical values with nothing.

Example:

document.write('teste 1 teste'.replace(/[\d]/g, ''));

The solution to your problem would be doing something like this:

var valor = document.querySelector('#id_do_span').innerHTML

var outro_valor = 15;

parseInt(valor) + outro_valor

See it working on JSFIDDLE

If you want to use regular expressions to only obtain numerical values, use the regular expression \D+ (non-numerical).

Behold:

var string = 'teste 15 teste';

document.write(string.replace(/\D+/g, ''));

Suggestion: As a suggestion, I suggest you use the values of data of jQuery. You can set the value Number usually there, for he is not saved as String. This way you avoid unnecessary value conversions.

Example:

$(function ()
{
    $('body').html($('#meu_span').data('number') + 3);
})
<span id="meu_span" data-number='3'>3</span>

  • +1 and follows a fiddle to help :)

  • So I did it but it didn’t work, NAN appears, look at my code now on the question!

  • See if the edition pleases you. I put an option with the regular expression.

  • Guys now know why this giving error was shitted my, I’m catching no span value now that I saw . Thanks to all!

2

To avoid problems of empty values I use this way

numeroRecuperado = $('#qualquer span').text().replace(/\D/g, ''); // Remove o que não é número
numeroRecuperado = parseInt(numeroRecuperado) | 0; // o | 0 evita que uma falha na conversão retorne um NaN
numeroQualquer = 5;

total = numeroRecuperado + numeroQualquer;

$("#qualquer span").text(total);
  • Dude, I didn’t even know that :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.