Grcn Grcn - 6 months ago 177
Javascript Question

Get attribute values with JQuery issue (Uncaught TypeError: Cannot use 'in' operator to search for 'length' in..)

I am trying to get attributes values with JQuery but I am getting error which is: jquery.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in PROD

I saw some advises which are about the downgrade JQuery version to 1.10.xx but I am getting this error with all versions of JQuery.

Do you any advise?

Thanks in advance..

My code is below:

<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>

<body>
<input test="PROD" value="2" type="text">
<input test="DEV" value="3" type="text">
<input type="button" value="Write It" onclick="writeIt()"/>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
function writeIt() {
$.each($('input').attr('test'), function () {
var testdata = $(this).attr('test');
//$(this).val(testdata);
console.log(testdata);
});

}
</script>

Answer

you can try

<script>
function writeIt() {
    $('input:not([type="button"])').each(function () {
        var testdata = $(this).attr('test');
        //$(this).val(testdata);
        console.log(testdata);
    });

}
</script>

Demo

function writeIt() {
    $('input:not([type="button"])').each(function () {
       var testdata = $(this).attr('test');
       //$(this).val(testdata);
       console.log(testdata);
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input test="PROD" value="2" type="text">
<input test="DEV"  value="3" type="text">
<input type="button" value="Write It" onclick="writeIt()"/>