Jeyrus
Jeyrus

Reputation: 23

Uncaught TypeError: Cannot read property

I got this error: Uncaught TypeError: Cannot read property 'd' of undefined

and it's on the var d section.

function sign() {
    var d = document.first.d.value;
    var mon = document.first.mon.value;
    var y = document.first.y.value;
    var curd = new Date(y,mon-1,d);
    var res2 = curd.getMonth();
    var i = 0;
}

I don't know what's the problem. Can anyone help me out. I can detail it more if you need some. Thanks in advance.

Upvotes: 0

Views: 608

Answers (1)

meder omuraliev
meder omuraliev

Reputation: 186562

That's an oldschool DOM 0 accessing code where elements were referenced by name="" eg name="first" which is deprecated. Use document.getElementById instead of document.first.d.

<input id="d" value="10" />
<input id="mon" value="02" />
<script>
(function() {
var first = document.getElementById('d');
alert(first.value);
})();
</script>

Your code fails because it doesn't get a reference to document.first because theres probably no name=first but maybe an id=first. Make those 3 statements use gEBI and set ids if you need to, and you should be set.

Upvotes: 2

Related Questions