javascript - indexof() not selecting the first instance of string -


been trying figure out time , can't seem it.

i have casperjs

    var size = this.evaluate(function () {     var  number = 10;              var size;             $('select[name="placeholder"] option[name="plid"]')                 .each(function (index, dom) {                     var currentsize = $(dom).attr('value');                     if (string(currentsize).indexof(                         string(number)) > -1) {                         size = currentsize;                     }                 });             return size;         }); 

and i'm trying select 10 value

<option name="plid" value="25437456:10" data-label="(10)">10</option> <option name="plid" value="53467763:10.5" data-label="(10.5)">10.5</option> 

but reason selects 10.5 every time, when should select 10.

anyone know what's wrong? appreciate help.

the .each() loop processes every element. size variable end being last item matched rather first.

you can stop loop have match returning false within function passed .each().

var size = this.evaluate(function() {       var number = 10;        var size;       $('select[name="placeholder"] option[name="plid"]')         .each(function(index, dom) {           var currentsize = $(dom).attr('value');           if (string(currentsize).indexof(             string(number)) > -1) {             size = currentsize;             return false;  // <--- add           }         });       return size;     }); 

Comments