D
D
Dima Pautov2015-10-29 10:38:46
JavaScript
Dima Pautov, 2015-10-29 10:38:46

How to get a dynamically changing number from a string?

Good morning! Help me to understand. There is an ajax function that returns html as a string.

<div class="h-right">
<div class="h-lk"><a href="/registratsiya/">Регистрация</a> / <a href="/auth/">Вход</a></div>
<div class="h-basket">
<a href="#">Корзина (<span id="count_prod">3</span>)</a>
</div>

<span id="count_prod">3</span> Here the triple is a counter, which is always different. How it is possible to receive this number from this line? Help me please!
PS I looked in the direction of regular expressions, but something did not work out for me. I don't know them very well.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kravchenko, 2015-10-29
@bootd

if using jquery:

var html = '<div class="h-right">
<div class="h-lk"><a href="/registratsiya/">Регистрация</a> / <a href="/auth/">Вход</a></div>
<div class="h-basket">
<a href="#">Корзина (<span id="count_prod">3</span>)</a>
</div>';
$(html).find('#count_prod').text()

S
Sergey Voronkov, 2015-10-29
@no_smoking

here's a regular

var reg = /<span id="count_prod">([\d]+)<\/span>/img;
   var result = reg.exec(html);
   var count_prod = result[1];
   alert(count_prod)

I
inDeepCode, 2015-10-29
@inDeepCode

var node = document.getElementById('count_prod');

/* textContent - ваша цифра */
 textContent = node.textContent;

/* выводим ее алертом например */
 alert(textContent);

/* или в консоль */
 console.log(textContent);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question