0

I'm building a tagger. After a user submits a tag, ajax returns:

{"returnmessage":"The Ajax operation was successful.","tagsinserted":"BLAH, BLOOOW","returncode":"0"}

I want to take the tagsinserted and loop through it, and during each loop take the item in the list and insert it on the HTML page. suggestion on how to do this right?

Here is the current code:

$("#tag-post").click(function(){
    // Post $('#tag-input').val()
    $.ajax({
        url: '/tags/ajax/post-tag/',
        data: { newtaginput : $('#tag-input').val(), userid : $('#userid').val()},
        success: function(data) {
            // After posting
            alert('done');

        }
    });     

});

2 Answers 2

1

You can do something like this:

$("#tag-post").click(function(){
  $.ajax({
    url: '/tags/ajax/post-tag/',
    data: {newtaginput : $('#tag-input').val(), userid : $('#userid').val()},
    success: function(data) {
      $.each(data.tagsinserted.split(', '), function(i, v) {
        $("<div></div>").text(v).appendTo("#tagHolder");
      });
    }
  });
});
Sign up to request clarification or add additional context in comments.

6 Comments

I like this. strange.. Here is the response:{"returnmessage":"The Ajax operation was successful.","tagsinserted":"adadada","returncode":"0"} I'm getting a tagsinserted is undefined?
@nobosh - Add the dataType: 'json', option to your $.ajax call and alert(data); to your success call, what do you get?
It's still saying "data.tagsinserted is undefined" in Firebug.... Here is the response again: {"returnmessage":"The Ajax operation was successful.","tagsinserted":"tettttt","returncode":"0"}
For Alert Data, it's showing up right I think: {"returnmessage":"The Ajax operation was successful.","tagsinserted":"tettttt","returncode":"0"}
alert(data.tagsinserted); is altering undefined too
|
0

You can loop through the tags by calling data.tags.split(',') and looping through the array it returns.

You can insert tags to the page by calling $('<li />').text(tag).appendTo('someSelector').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.