jim
jim

Reputation: 23

how to get the value of a span tag?

I want to get the contents of a span tag but what I have isn't working. An alert shows an empty dialog box. I've tried the following but nothing works.

var test=$('#test').val()
var test=$('span#test').val()
var test=$('td span#test').val()

Can someone tell me what I'm doing wrong?

Upvotes: 2

Views: 14211

Answers (6)

Abdul Kader
Abdul Kader

Reputation: 5842

You should use .text(). .val() is only for input elements.

var test = $('span#test').text();

Upvotes: 0

itb564
itb564

Reputation: 223

You are correct almost, but instead of the val() to Need to say html().

 var test=$('#test').html();
 var test=$('span#test').html();
 var test=$('td span#test').html();

Upvotes: 1

DanielB
DanielB

Reputation: 20210

$('span#test').text()

will give you the text. val() is for the attribute value

Upvotes: 1

alex
alex

Reputation: 490143

span elements do not have a value property.

Instead, use html() for the HTML or text() for the text nodes.

Upvotes: 7

Govind Malviya
Govind Malviya

Reputation: 13743

use this

var test=$('#test').html()

Upvotes: 0

netbrain
netbrain

Reputation: 9304

a span doesn't have a value attribute.

You should instead do the following

var test=$('span#test').html()

or

var test=$('span#test').text()

Upvotes: 0

Related Questions