[jquery] Jquery split function

I have a ajax function as below

<script type="text/javascript">
$(document).ready(function () {

var timer, delay =600000; //5 minutes counted in milliseconds.

timer = setInterval(function(){
    $.ajax({
  type: 'POST',
  url: 'http://localhost/cricruns-new/index.php/score-refresh/fetchdes/index/86/1',
  success: function(html){

    alert(html);
  }
});

},delay);

});
</script>

it outputs the following

total:-370:-wickets:-4:-overs:-50.0:-striker:-Yusuf Pathan:-sruns:-8:-sballs:-10:-sfour:-0:-ssix:-0:-nonstriker:-Virat Kohli:-nsruns:-100:-nsballs:-83:-nsfours:-8:-nssix:-2

i want to split the result using :- and i have to assign even values to the div which has the id as the odd value..

This question is related to jquery

The answer is


Try this. It uses the split function which is a core part of javascript, nothing to do with jQuery.

var parts = html.split(":-"),
    i, l
;
for (i = 0, l = parts.length; i < l; i += 2) {
    $("#" + parts[i]).text(parts[i + 1]);
}

Javascript String objects have a split function, doesn't really need to be jQuery specific

 var str = "nice.test"
 var strs = str.split(".")

strs would be

 ["nice", "test"]

I'd be tempted to use JSON in your example though. The php could return the JSON which could easily be parsed

 success: function(data) {
   var items = JSON.parse(data)
 }