Brendan
Brendan

Reputation: 155

jQuery, get the value in a label

I'm trying to get the value of this label:

  <label id="name" class="sub_heading"><?php echo $_SESSION['lastname'].", ".$_SESSION['firstname']; ?></label>

for some reason this:

  var name = ("#name").val();

is not working?

Upvotes: 1

Views: 84

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191729

Per documentation, .val() should be used only on form controls (input, button, select, textarea). If you want the contents of an element, use .text() or .html() as appropriate.

Upvotes: 0

VisioN
VisioN

Reputation: 145378

Use either html() or text() as follows:

var name = $("#name").html();

Method val() is suitable only for form elements (e.g. <input>, <select>, etc).

Upvotes: 8

Related Questions