0

I have list generated by ajax call like this

<ul id="list">
   <li name="A">some val</li>
   <li name="B">some val</li>
   <li name="C">some val</li>
</ul>

with a setinterval, the same ajax call will create a list like this

<ul id="listClone">
  <li name="A">some new val</li>
  <li name="B">some cccc val</li>
  <li name="C">some ddd val</li>
</ul>

after I get the listClone, I need to replace the list "A" with listClone A.

How do I do that with jQuery?

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
tv4free
  • 287
  • 3
  • 17
  • I'm not sure if you are trying to replace the entire list or just the `li` element with `name=A` but if it is the latter that's what my answer addresses – lucuma Jun 02 '12 at 03:43

4 Answers4

2

I believe you just want to replace the li element:

$('#list li[name=A]').html($('#listclone li[name=A]').html());
lucuma
  • 18,247
  • 4
  • 66
  • 91
0

$('#list').replaceWith('<ul id="listClone"><li name="A">some new val</li><li name="B">some cccc val</li><li name="C">some ddd val</li></ul>');

Suggestion:

Try keeping the new list id as 'list` rather than 'listClone' since this will NOT work the next time you call the code.

PhD
  • 11,202
  • 14
  • 64
  • 112
0
$("#list").html($("#listClone").remove().html());

listClone is removed. and list's html is replaced with listClone's html.

Curry
  • 885
  • 5
  • 15
0

To remove whole #list with #listClone you can do:

$('#list').replaceWith($('#listClone'));

To remove a particular list item of #list with a #listClone item you can do:

$('#list').eq(0).replaceWith($('#listClone').eq(0)); // replace first element

or using name attribute

$('#list li[name=A]').replaceWith($('#listClone li[name=A]')); // replace first element

Also do with .html():

$('#list li[name=A]').html($('#listClone li[name=A]').html()); // replace first element

Also can use .text()

$('#list li[name=A]').text($('#listClone li[name=A]').text()); // replace first element
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164