Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array split using jquery?

i have some value stored in array and i wana split them and wana know length of its contains value but when i am running function it is not working

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

$(function(){


    var valData= ['songs','video','movie','games','other'];

    var valNew=valData.split(',');

    for(i=0;i<valNew.length;i++);

    alert(valNew.length)

    })


</script>


</head>

<body>

<select id="me"></select>
</body>
like image 837
Jitender Avatar asked Apr 21 '12 09:04

Jitender


1 Answers

Split is used to separate a delimited string into an array based upon some delimiter passed into the split function. Your values are already split into an array. Also your for loop syntax is incorrect.

Split Documentation: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

Corrected code:

$(function(){
    var valData= "songs,video,movie,games,other";

    var valNew=valData.split(',');

    for(var i=0;i<valNew.length;i++){
        alert(valNew.length)
    }
});

http://jsfiddle.net/tmHea/

like image 165
Kevin Bowersox Avatar answered Sep 19 '22 11:09

Kevin Bowersox