Demsaas Demsaas - 4 months ago 13
Ajax Question

Create ul li with jquery and put it in the html append

I got input and textarea, so problem is that I'mm sending ajax with some text and words and counting how much inputs have word in a text, and sending back list of objects of class which have 2 vals:quantity and word,and then i want to show a result like that:"word"-quantity" and these all things i need to put in ul li,so i tried to created ul and li with jquery but still cant get how to put it in my html append now with vals of list.
Thats my part of code:

function makeulli() {
var sub_ul = $('<ul/>');
$.each(data, function (i) {
var sub_li = $('<li/>');
$(sub_li).appendTo(sub_ul);
});
}

$(".texnwords").append("<section class='searchresult'><h4>Result:</h4><p>Words: <p>Text:<br>" + text + "</p></section>");


So i just need to put this created ul and li's in my append with values from list.

Answer

Not exactly sure what you're trying to do, but if you're trying to access the data variable you can do like this:

function makeulli() {
   var sub_ul = $('<ul/>');
   $.each(data, function (index, value) {
       // access like value.yourvalue
       var sub_li = $('<li/>');
       sub_ul.append(sub_li);
   });   
   $(".texnwords").append(sub_ul);         
}

Update:

Guess you mean like this?

function makeulli() {
   var sub_ul = $('<ul/>');
   $.each(data, function (index, value) {
       var sub_li = $('<li/>').html(value.word+'-'+value.quantity);
       sub_ul.append(sub_li);
   });   
   $(".texnwords").append(sub_ul);         
}

If in doubt, how the data structure for the variable is, then try console.log() either the "value" or "data".

Comments