doniyor
doniyor

Reputation: 37894

why this jquery isnot selecting?

sorry for asking this simple question, but i am stuck, why is this jquery not taking the value, it is alerting empty message.

my jquery:

$(function(){
  $('dt').on('click',function(){
     alert($(this).val());
  });
});

my html:

<dl>
<dt>test</dt>
<dd>- test</dd>
</dl>

i am expecting it to alert the test message. but it is alerting without message. $(this).val() must be right, i think. thanks for help and guidance..

Upvotes: 0

Views: 54

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

It is because val() relates to the value property of an input field. <dt> is not an input field. Try doing

$(function(){
  $('dt').on('click',function(){
     alert($(this).text());
  });
});

Upvotes: 3

Mike Robinson
Mike Robinson

Reputation: 25159

.val() is for input elements, you need to use .text()

alert($(this).text());

or

alert($(this).html()); // Careful, since it will include markup (obviously)

Upvotes: 3

Related Questions