2

So I was looking on some SO questions and the jQuery area to try to use the similar feature in PHP using explode which will just explode a string given into it's pieces from a type of substring.

This is the code I used:

var users = // AJAX CALL TO GET USERS
var usersArr = users.split();

I looked at the w3 tutorial for splitting strings and this was the JavaScript type, and even that didn't work.

Error message:

index.js:45 Uncaught TypeError: users.split is not a function

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Jack Hales
  • 1,574
  • 23
  • 51
  • 4
    split() should be split(',') or the delimiter of your choice –  May 18 '16 at 10:07
  • 4
    AJAX is `asynchronous`!. – Sandeep Nayak May 18 '16 at 10:07
  • What output did you get and where there any errors in the console? – Oisin May 18 '16 at 10:07
  • It should be `.split('')` (empty string). But make sure that your ajax request has returned from server (common pit fall) – Justinas May 18 '16 at 10:08
  • 2
    Could you post a full code sample. If you're doing `var users = $.ajax();` then `users` will be a deferred object, not a string, hence the error you get. You should use the callback of the AJAX request. Also `split()` requires a parameter to break the string by. – Rory McCrossan May 18 '16 at 10:08
  • You need to add delimiter in `split` like `split("_")` – simon May 18 '16 at 10:08
  • `I looked at the w3 tutorial for splitting strings and this was the JavaScript type, and even that didn't work.` Never use W3Schools for anything. Their articles are often outdated and as you've seen, occasionally completely wrong. Always use [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript) – Rory McCrossan May 18 '16 at 10:10
  • [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) not quite a duplicate but one of your issues – Liam May 18 '16 at 10:10
  • I have given it a paramater, the `users` variable is gotten by AJAX getting the usernames and dispalying them by `
    `, I want to remove the `
    ` from the string so that I only have the usernames.
    – Jack Hales May 18 '16 at 10:11
  • Also see [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam May 18 '16 at 10:15

2 Answers2

7

just like PHP you can do but with different method.

var str = "How are you doing today?"
var res =  str.split(" ");

access this way : "String :" + res[0]+" "+res[1]+" "+res[4]+" "

output : How are today?

pankaj
  • 1
  • 17
  • 36
2

Since Ajax is asynchronous, you have to set users variable inside the success callback of the Ajax call and then split the string.

It can be don like this.

$.ajax({
    url: "url-to-the-page",
    success: function(data) {
        var users = data;
        var usersArr = users.split(";"); // if semicolon is the separator.
    }
});
ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35