-3

I have a problem with my condition: Uncaught TypeError: Cannot read property 'length' of undefined It is logic, but how can i fix this?

Thank you.

<script>
    $(window).scroll(function () {
        var elementms = $("#testms").attr("john");
        if (elementms.length) {

            $({
                someValue: 0
            }).animate({
                someValue: 500
            }, {
                duration: 3000,
                easing: 'swing', // can be anything
                step: function () { // called on every step
                    // Update the element's text with rounded-up value:
                    jQuery('#el').text(commaSeparateNumber(Math.round(this.someValue)));
                }
            });

            function commaSeparateNumber(val) {
                while (/(\d+)(\d{3})/.test(val.toString())) {
                    val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
                }
                return val;
            }
        }
    });     
</script>
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • Are you sure your `id="testms"` has attribute named `john`? – Justinas Aug 17 '15 at 14:04
  • 1
    Without seeing the html, it's possible you just need to wrap your JS in a `$(document).ready(function(){ ... });` – neilsimp1 Aug 17 '15 at 14:04
  • 4
    That is some funky HTML if you really have an attribute named `john`. – James M Aug 17 '15 at 14:04
  • this attribute "john" is just a test ;) my condition is write, but when i test a non-exist attribute, i have this error. – Alan Torre Aug 17 '15 at 14:06
  • @AlanTorre yes of course, If an element has no attribute `john` the function `.attr()` will return `undefined` – empiric Aug 17 '15 at 14:12
  • I use ScroolReveal.js. Sorry it's to complicate. I just want to know, how test an attribute who don't exist nohting this error : Cannot read property 'length' of undefined. This attribute, appear when i scroll. – Alan Torre Aug 17 '15 at 14:12
  • If your goal was to test if an attribute exist, then you should rephrase your question. http://stackoverflow.com/questions/1318076/jquery-hasattr-checking-to-see-if-there-is-an-attribute-on-an-element – Zsw Aug 17 '15 at 14:17

1 Answers1

0

perhaps a simple check to see if elementms is defined would suffice?

if (typeof elementms !== 'undefined' && elementms.length) {
    …
}

If elementms is now undefined it won't go looking for a property length.

I personally prefer a less tabbed solution, and check the other way around.

$(window).scroll(function () {
    var elementms = $("#testms").attr("john");
    if (typeof elementms === 'undefined' || elementms.length === 0) {
        return;
    }

    $({
        someValue: 0
    }).animate({
        someValue: 500
    }, {
        duration: 3000,
        easing: 'swing', // can be anything
        step: function () { // called on every step
            // Update the element's text with rounded-up value:
            jQuery('#el').text(commaSeparateNumber(Math.round(this.someValue)));
        }
    });
});

function commaSeparateNumber(val) {
    while (/(\d+)(\d{3})/.test(val.toString())) {
        val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }
    return val;
}
Pjetr
  • 1,372
  • 10
  • 20
  • Since he is not checking for a specific number returned of `length` a simple `if (elementms) {` should be enough. – empiric Aug 17 '15 at 14:16
  • I find it strange to downvote a strict answer to give a less strict, ergo, more error-prone suggestion. Just becaus it works and is shorter doesn't mean it's better. But that's just my opinion… – Pjetr Dec 15 '15 at 10:40