Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  96] [ 6]  / answers: 1 / hits: 94663  / 12 Years ago, sat, april 21, 2012, 12:00:00

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>

More From » jquery

 Answers
30

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/


[#86098] Thursday, April 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynnb

Total Points: 402
Total Questions: 96
Total Answers: 109

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
kaitlynnb questions
;