joseep
joseep

Reputation: 13

Append span to prev a

I have a little problem.

<li>
    <a></a>
    <span>Description 1</span>
</li>

<li>
    <a></a>
    <span>Description 2</span>
</li>

I want to append the span to the prev a without using a class or the content. Can you help me?

I tried something like this, but then I copy the span into each <a> tag:

$('li span').appendTo($('li span').prev());

Thanks for any help!

Upvotes: 1

Views: 104

Answers (3)

Sergi Juanola
Sergi Juanola

Reputation: 6647

Try this:

​$('li').each(function(i, li) {
   $(li).find('a').append($(li).find('span'));
});​​​

jsfiddle. I added some colors and behaviors to show you it worked without looking at the dom

Upvotes: 0

Kevin B
Kevin B

Reputation: 95057

You could do it this way,

​$("li a").append(function(){
   return $(this).next(); 
});​​​​​

http://api.jquery.com/append/

http://jsfiddle.net/JH8pg/

Upvotes: 0

sbeliv01
sbeliv01

Reputation: 11840

Is this what you're looking to accomplish?

$('li span').each(function(){
    $(this).appendTo( $(this).prev() );
});

http://jsfiddle.net/rRsdz/

Upvotes: 1

Related Questions