tv4free
tv4free

Reputation: 287

replacewith jquery

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?

Upvotes: 0

Views: 1030

Answers (4)

thecodeparadox
thecodeparadox

Reputation: 87073

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

Upvotes: 0

lucuma
lucuma

Reputation: 18339

I believe you just want to replace the li element:

$('#list li[name=A]').html($('#listclone li[name=A]').html());

Upvotes: 2

Curry
Curry

Reputation: 895

$("#list").html($("#listClone").remove().html());

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

Upvotes: 0

PhD
PhD

Reputation: 11334

$('#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.

Upvotes: 0

Related Questions