b11 b11 - 1 year ago 65
Javascript Question

How to get value of integer element in JQuery

I am new to JQuery and dynamically appending a value to an h1 element using a span element

<h1>Height: <span class="height"></span></h1>

Now I want to take the value of the span element and use it somewhere else. I tried doing this in JQuery but it's no good.

var height = $(".height").val();

What is the right way instead?

Answer Source

A span element doesn't have a "value", form elements do. (input, select, etc.) If you're looking to get the text of the span element, you can do that like so:

var height = $(".height").text();

Note: If there are multiple matching elements, this will concatenate the text of all of them. Observe.