views:

37

answers:

1

Sorry for my bad english, but I have a simple db with some values lets say: one, two , three and four...

Now I have a list like this with an id "test-id" :

<ol id="**test-id**" >
    <li>**test1**</li>
    <li>**test2**</li>
    <li>**test3**</li>
    <li>**test4**</li>
</ol>

In the php part I can get every value from my db but then I want to append them to every element...like: test1 and append the first value from my db, then test2 and append the second value from my db.

I'm stacked with JQuery.

+1  A: 

This doesn't really seem like a jQuery question. However, regardless of whether you're creating this ID string in PHP or javascript, you can simply concatenate "test" with (for example) $row['id'] in PHP or row.id in JS. In PHP, the concat operator is a full stop "." and in Javascript it's a + sign.

Edit: a simple example to illustrate looping through for an append. Here I'm assuming you want the ID of each LI to be a value equal to test1 where 1 is the ID number such that id #2 looks like: test2.

var appendString = "";
var dataArray = new Array();
dataArray[0] = 1;
dataArray[1] = 2;
dataArray[2] = 3;
dataArray[3] = 4;

$.each(dataArray, function(i, val) {
    appendString += '<li id="test' + val + '">ID ' + val + '</li>';
});

$('#mylist').append(appendString);

This can be viewed in action here: http://jsfiddle.net/VFFUj/2/

It's important to note that appending a full string like this in jQuery is actually faster than appending each LI on its own, so it's always best, if at all possible, to create your html string before you append it to the document.

treeface
i've got a trigger in JQuery and when someones clicks a list element then it appends the value from the db...so i thought i should parse the value from the db to Jquery append() method
t0s
Oh...well sure. Ignoring whence you get the value, you can simply concat it to your append string. See my answer for a simple example.
treeface