How to get number inside a div with jquery

后端 未结 2 723
离开以前
离开以前 2021-01-18 19:05

How do I get a number inside a div and use it as a number in JavaScript/jQuery? Here is what I have:

HTML:



        
相关标签:
2条回答
  • 2021-01-18 20:04
    var numb = $('.number').text();
    console.log(parseInt(numb));
    
    0 讨论(0)
  • 2021-01-18 20:05

    .val() is for getting the value of form elements like input, select, etc. You can't use it on divs. You can use .text() to get the text content of the div element.

    var number = $('.number').text();
    

    But it will return the content as a string. To convert it to a Javascript number, you have to use parseInt():

    var number = parseInt($('.number').text());
    
    0 讨论(0)
提交回复
热议问题