Reputation: 23
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
Reputation: 5842
You should use .text()
. .val()
is only for input elements.
var test = $('span#test').text();
Upvotes: 0
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
Reputation: 20210
$('span#test').text()
will give you the text. val() is for the attribute value
Upvotes: 1
Reputation: 490143
span
elements do not have a value
property.
Instead, use html()
for the HTML or text()
for the text nodes.
Upvotes: 7
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