1

I need to get a list of comma-separated values from jQuery ajax data returned from the server. The data I am getting is as below

<div id="1" class="clicked">A</div>
<div></div>
<div id="2" class="clicked">B</div>
<div></div>
<div id="3" class="clicked">dsf</div>
<div></div>
<div id="4" class="clicked">fd</div>
<div class=users>[email protected],[email protected]</div>

This is the data that I get when the alert it. I need to extract the div with class = users and update it to another div as lists. How is it possible.. Please help

2

1 Answer 1

3

Something like this works. We use $ to cast the string into a jQuery object. Then you can use normal jQuery calls like filter to get the list of emails.

Then create a list. Then append each email to it.

http://jsfiddle.net/BCmqL/3

//just hardcode in an example of your data
var ajaxData = '<div id="1" class="clicked">A</div><div></div><div id="2" class="clicked">B</div><div></div><div id="3" class="clicked">dsf</div><div></div><div id="4" class="clicked">fd</div><div class=users>[email protected],[email protected]</div>';

//turn it into a jQuery object, now you can just use jQuery to do stuff
$jObject = $(ajaxData);
$emails = $($jObject).filter('div.users').text();

//split the list to get all the emails
var emails = $emails.split(',');

//create a list to add list items to
var $ul = $('<ul />');

//iterate through list and append them to a ul
$(emails).each( function(index, value) {
    var $li = $('<li>'+value+'</li>');
    $ul.append($li);
});

//append new list to body
$('body').append($ul);
​
Sign up to request clarification or add additional context in comments.

1 Comment

It worked!! Thanks.. Btw why do we need to convert it into a jquery object first. Why can't i use $(data).find('#users').text();

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.