<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){var mainVal = $("#h1").valueOf();alert(mainVal);})</script>
</head>
<body>
<h1 id="h1" value="0">hallow you </h1>
</body>
</html>
I am trying to use the val() method here. Any help would be awesome. Thank you.
first, you've use .valueOf()
(its a different js methoud and here is its docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf ) it should be .val()
which is used to get the values of form
elements such as input
, select
and textarea
, for more info check the docs here: https://api.jquery.com/val/
second, to get the value from the value
attribute in an element you need to use the .attr('attribute-name')
method
$(document).ready(function () {
var mainVal = $("#h1").attr("value");
alert(mainVal);
})