Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery split function

Tags:

jquery

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..

like image 762
oldrock Avatar asked Feb 23 '11 09:02

oldrock


2 Answers

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]);
}
like image 65
nickf Avatar answered Jan 04 '23 09:01

nickf


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)
 }
like image 37
Ben Griffiths Avatar answered Jan 04 '23 08:01

Ben Griffiths