Answer the question
In order to leave comments, you need to log in
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>
Answer the question
In order to leave comments, you need to log in
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()
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)
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 questionAsk a Question
731 491 924 answers to any question