Chipe
Chipe

Reputation: 4811

How to get number inside a div with jquery

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

HTML:

<div class="number">25</div>

jQuery:

var numb = $('.number').val();
console.log(numb);

jsfiddle: http://jsfiddle.net/nw9rLfba/

Upvotes: 6

Views: 21282

Answers (2)

Arnelle Balane
Arnelle Balane

Reputation: 5497

.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());

Upvotes: 20

Wojciech Mleczek
Wojciech Mleczek

Reputation: 1634

var numb = $('.number').text();
console.log(parseInt(numb));

Upvotes: 5

Related Questions