1

Why my split and join are not working??

$.getJSON(url, function (data) {
    // create ".tags" elements and populate them with data
});
$(".tags").text(function (i, val) {
   return val.split(",").join(", ");
});

Fiddle

I just simply want to add space after every ',' (comma).

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Richa
  • 4,240
  • 5
  • 33
  • 50
  • 3
    You are splitting it before getting any result from JSON response. – GoodSp33d Jun 30 '14 at 06:08
  • 3
    Using AJAX is a pretty important detail to leave out of the question. – Ja͢ck Jun 30 '14 at 06:09
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Phil Jun 30 '14 at 06:10
  • @Phil Its not a dup of that linked question. Theres no return value here. – GoodSp33d Jun 30 '14 at 06:11
  • Thanks @GoodSp33d .. i know its silly one.. :P – Richa Jun 30 '14 at 06:11
  • 1
    @GoodSp33d The implementation is slightly different but the [accepted answer](http://stackoverflow.com/a/14220323/283366) outlines the problem and solution – Phil Jun 30 '14 at 06:12
  • but why down votes for this question .. x( – Richa Jun 30 '14 at 06:13
  • 1
    @Era because you left out the most important details – Phil Jun 30 '14 at 06:14
  • But that question is totally different from mine.. and Phil if i could have any idea of my mistake then i surely didnt ask this question here. what you say @GoodSp33d – Richa Jun 30 '14 at 06:16
  • @Era read the accepted answer on that post (all of it) and you will understand where you went wrong and how you can fix the problem – Phil Jun 30 '14 at 06:18
  • 1
    @Era Nobody is blaming you for asking a question that's been asked before (in fact, this particular question is asked almost every day); what's important, though, is to write the smallest code to reproduce the issue **in the question itself**. – Ja͢ck Jun 30 '14 at 06:19
  • But Phill .. before searching for solution, you must know the problem behind this.. and i was not aware about the issue.. – Richa Jun 30 '14 at 06:20
  • No.. I am not talking about blaming .. i m just worried about down votes.. :( – Richa Jun 30 '14 at 06:21
  • @Era getting marked as a duplicate isn't a bad thing. It just reduces clutter on StackOverflow – Phil Jun 30 '14 at 06:21
  • Anyways.. Thanks all for your valuable time. – Richa Jun 30 '14 at 06:24

3 Answers3

7

As $.getJSON is async call, you need to perform your operation in callback method of $.getJSON

$.getJSON('http://www.json-generator.com/api/json/get/bXClHuexaq?indent=2', function (data) {
  //... your code

    $(".tags").text(function (i, val) {
        return val.split(",").join(", ");
    });
 });

Fiddle

Satpal
  • 132,252
  • 13
  • 159
  • 168
3

As Satpal had mentioned using $.getJSON is required

$.getJSON(url, function (data) {
    // code
    $(".tags").text(function (i, val) {
        return val.split(",").join(", ");
    });
 });

However, as a general practice I'd recommend to use regular expression instead, mainly because for longer strings arrays need more memory

$(".tags").text(function(i, val) {
    return val.replace(/,/g, ", ");
});
vasa
  • 787
  • 8
  • 21
0

Place your split logic in callback.Check out the following jsfiddle

http://jsfiddle.net/8TdZ3/6/

$(document).ready(function (e) {
    $.getJSON('http://www.json-generator.com/api/json/get/bXClHuexaq?indent=2', function (data) {
        var output = "<table width='100%' cellspacing='0' cellpadding='0'>";
        output += "<tr>";

        output += "<th width='20%'>" + 'Tags' + "</th>";
        output += "<th width='15%'>" + 'Friends' + "</th>";
        output += "</tr>";
        for (var i in data) {
            output += "<tr>";

            output += "<td class='tags'>" + data[i].tags + "</td>";
            output += "<td>";
            for (var j = 0; j < data[i].friends.length; j++) {
                output += "<div>" + data[i].friends[j].name + "</div>";
            }
            output += "</td>";
            output += "</tr>";
        }
        output += "</table>";

        document.getElementById("placeholder").innerHTML = output;
          $(".tags").text(function (i, val) {
        alert(i);
        return val.split(",").join("#");
    });

    });

});
santosh singh
  • 27,666
  • 26
  • 83
  • 129