-2

for example I have a list of li's like this:

<li id='r1' class='myclass'>something</li>
<li id='r2' class='myclass'>something</li>
<li id='r3' class='myclass'>something</li>
<li id='r4' class='myclass'>something</li>
<li id='r5' class='myclass'>something</li>

I want to use a function for replace two li with each other.

for example: put li2 o first and put li1 on second.

and I have a function like this:

function updown(li_id , dir)
{
 //what should i type here....
}

li_id: is my li id

dir: is "up" or "down"

Poya Eraghi
  • 125
  • 1
  • 8
  • 1
    This is pretty thin, as you're not showing any effort of your own. I would *bet* there is plenty of stuff when Googling, say, `jquery change order of html elements`. – Pekka Dec 09 '12 at 17:59
  • I have try for that but I didn't see any reason to make my question difficult and complicate. – Poya Eraghi Dec 09 '12 at 18:06
  • 1
    [What have you tried?](http://whathaveyoutried.com) As someone who is still fairly new to jQuery, what you're trying to do is not difficult. Put forth some effort. –  Dec 09 '12 at 18:10

1 Answers1

2

use before() or after() to swap the order of two element :

$('#r1').before($('#r2'));​

FIDDLE

or your function:

function updown(li_id , dir) {
    $('#'+li_id)[dir!='up'?'before':'after']($('#'+li_id)[dir!='up'?'next':'prev']());
}

use like:

updown('r3', 'up');
updown('r1', 'down');

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • yes. next,prev,before,after. but for example there is not any prev li or what can i do with html id conflict – Poya Eraghi Dec 09 '12 at 18:10
  • 1
    ID's are unique, there is no ID conflict unless you do a big no-no and create one. As for non existing previous or next element, you can't swap places with something that are'nt there ? – adeneo Dec 09 '12 at 18:22
  • TNX, It worked. and I also used jquery.sortable. is it supported by old browsers? – Poya Eraghi Dec 10 '12 at 07:28