The Problems
.val()
is used to get the value of an input element - i.e. what the user has typed into a text box. It won't work for this purpose.
However, it should still be defined, even if called on an HTML element (it would still return null
, though, so you would still have a problem). The only reason for .val()
to be not a function would be if you had not included jQuery - insure that you have a line like
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
in the <head>
section of your webpage to include jQuery, otherwise none of the solutions below will work either.
The Solution
You want to get the internal HTML value of a <div>
. So, you should use .text()
or .html()
instead of .val()
.
.text()
will return the text in the <div>
, without any HTML tags;
.html()
will return all of the content of the <div>
, including any HTML tags inside it.
Example
console.log($(".sku-info").text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-3">
<div class="vou-col" style="cursor: pointer">
<h4>Free Large Bucket</h4>
<span class="sku-info">Voucher123</span>
</div>
</div>
$('.sku-info').text()
since a span object don't contain a value but only text – Carsten Løvbo Andersen Aug 3 '17 at 6:00