0
$.fn.swapWith = function (selector) {
    var placeholder = $('<div id="placeholder">').insertAfter(selector);
    $(selector).insertAfter(this);
    placeholder.after(this).remove();
};


var el = $('#4');
el.swapWith(el.nextAll('.' + el.attr('class') + ':first'));

@Box9: can i make this code to swap with previous div. what code change is needed

S L
  • 14,262
  • 17
  • 77
  • 116
kiran
  • 749
  • 2
  • 6
  • 5
  • @experimentX : can u help me in this ? – kiran Feb 07 '11 at 10:22
  • @kiran can you post your a bit of you html code and explain a bit pleaase – S L Feb 07 '11 at 10:26
  • @kiran code has been updated, and test as much you like it works, and if i can't help you then i seriously doubt anyone can help you – S L Feb 07 '11 at 11:16
  • P.S. I am going offline in a hour (for a week or so) i would be able to help you either for a week – S L Feb 07 '11 at 11:17
  • Yes you can use next().next() unless you are out of index i.e. you cannot do that after 3rd div – S L Feb 07 '11 at 12:07
  • All the best kiran with your project, try hard sure jquery is no so tough, and if my code was helpful, then don't forget to accept it ... catch you later ... after a week – S L Feb 07 '11 at 12:10

1 Answers1

0

You can try this. And this is the final one! Click on div to see the effect

<script src="jquery.js"></script>
<style>
    .outmost{
        width: 100%;
        height: 100px;          
        margin-top: 10px;
    }

    .inner{
        width: 100%;
        background: #A124C2;
        height: 20px;           
        margin-top: 10px;

    }

    #div1{  background: red; }
    #div2{  background: green; }
    #div3{  background: blue; }
    #div4{  background: yellow; }       
    #div5{  background: black; color: white; }      
</style>

<script>
$(function(){
    //function trigger on click of outer class
    $('.outmost').click(
        function(){
            $(this).insertBefore($(this).prev());
        }
    )
    //$('#div2').insertBefore($('#div2').prev());

})
</script>


<div id="div1" class="outmost">
    <h1>div 1</h1>
    <div class="inner">inner 1</div>
    <div class="inner">inner 1</div>    
</div>
<div id="div2" class="outmost">
    <h1>div 2</h1>
    <div class="inner">inner 2</div>
    <div class="inner">inner 2</div>    
</div>
<div id="div3" class="outmost">
    <h1>div 3</h1>
    <div class="inner">inner 3</div>
    <div class="inner">inner 3</div>    
</div>
<div id="div4" class="outmost">
        <h1>div 4</h1>
    <div class="inner">inner 4</div>
    <div class="inner">inner 4</div>    
</div>
<div id="div5" class="outmost">
    <h1>div 5</h1>
    <div class="inner">inner 5</div>
    <div class="inner">inner 5</div>
</div>
S L
  • 14,262
  • 17
  • 77
  • 116
  • im having other divs in the middle of these divs. so it will select that div. so im provided common class name to divs that i want to swap with. so how can i implement this – kiran Feb 07 '11 at 10:41
  • @kiran well try this http://stackoverflow.com/questions/4911998/swapping-div/4912029#4912029 if this is not what you want then don't show me your code ... just tell me clearly what you exactly want to do – S L Feb 07 '11 at 10:44
  • @experimentX : im having five divs each havina an id and a comman clasxs name . in the midde of each div there is another
    tag
    q
    q
    q
    q
    q
    i want to replace the div1 and q1 with the next div(div2 and q2). please help
    – kiran Feb 07 '11 at 11:00
  • @experimentX : help please really stuck – kiran Feb 07 '11 at 11:42
  • @kiran where did you get stuck... i am about to go offline? – S L Feb 07 '11 at 11:43
  • @experimentX : did u check my code sample , your above code is not working for me. – kiran Feb 07 '11 at 11:51
  • @kiran i checked you code and i don't know what it is doing, i rechecked my code and its working fine perhaps you didn't copy all code or did not add jquery or tried to add jquery like me (as i have set to jquery.js) or you changed some of your code. Well, this is working code, keep trying, but now i 've to go, c u week later – S L Feb 07 '11 at 12:01
  • @kiran also you can go to chat javascript and ask for help – S L Feb 07 '11 at 12:01
  • @kiran i hope you clicked on the main div – S L Feb 07 '11 at 12:03