JavaScript Is it possible to change the order of a div depending onchange function? possible to change the order

I am working on jquerymobiles. I have to change the order of displaying div depending on change of value in select box

html

<select class="sel">
      <option value="1">one</option>
       <option value="2">two</option>
        <option value="3">three</option>
        <option value="4">Four</option>
         </select>

          <div class="first">first</div>
             <div class="second">second</div>

jquery

 $(document).ready(function(){
        $( ".sel" ).change(function() {

        var val=$(this).val();
              if(val == 1)
            {

            $(".first").show();
                  $(".second").show();
                   }
         if(val == 2)
            {
           $(".second").show();
            $(".first").show();
                   }
          if(val == 3)
           {
       $(".first").show();
               $(".second").hide();
           }
        });
       });

I have to change the order of displaying div depending on the change of value.

If the user select option "two" then output should be.like this :

 second

 first

If the user select option "one" then output should be

first

 second

if the user select option "three" then it should display:

 first 

Please any one help me. Here is js fiddledemo

Answer:1

Demo Fiddle

If you have multiple if conditions, always use switch() cases

Use

  • .prependTo() : Insert content before. Though this gives to one time solution. You have to use insertBefore()
  • .detach() : Remove set of matched elements.

Try this

 $(document).ready(function () {
     $(".sel").change(function () {
         switch (this.value) {
             case '1':
                 $('.first').insertBefore('.second');
                 break;
             case '2':
                 $('.second').insertBefore('.first');
                 break;
             case '3':
                 $('.second').hide();
                 break;
         }

     });
 });
Answer:2

Use the following strategy: http://jsfiddle.net/2Ghqr/5/

Explanation:

When you encounter a situation where you want to reorder the DOM elements, we need to reattach them in the dom, you can pick up the element and then place them where you want to.

So here, I've simply picked first and second and placed them accordingly in a container.

As you can see, I have encapsulated the 'first' and 'second' div in a container called content.

<div class="content">
    <div class="first">first</div>
    <div class="second">second</div>
</div>

I have modified the JavaScript to the following:

 $(document).ready(function () {
     $(".sel").change(function () {

         var val = $(this).val();
         if (val == 1) {
            $('.content').append($(".first")).append($('.second'));
             $(".first").show();
             $(".second").show();
         }
         if (val == 2) {
             $('.content').append($('.second')).append($(".first"));
             $(".second").show();
             $(".first").show();
         }
         if (val == 3) {
             $(".first").show();
             $(".second").hide();
         }
     });
 });
Answer:3

I am making a simple mobile webpage where I have an image with an image map over it. I want the image to change when the user touches the image, and change back when the user lifts their finger. The ...

I am making a simple mobile webpage where I have an image with an image map over it. I want the image to change when the user touches the image, and change back when the user lifts their finger. The ...

  1. html image area map
  2. html image area
  3. html image area link
  4. html image area click
  5. html image area map generator
  6. html image area coordinates
  7. html image area map coords
  8. html image area clickable
  9. html image area selection
  10. html image area tag
  11. html image map area responsive
  12. html image map area border
  13. html image map area background color
  14. html image map area link
  15. html image map area style
  16. html highlight image area
  17. html make image area clickable
  18. html image active area
  19. html image map area color
  20. html image map area hover

I thought that given an object with controller & view properties, when the controller's state changed (or an m.prop property was changed) it would rerender the template. This isn't happening in ...

I thought that given an object with controller & view properties, when the controller's state changed (or an m.prop property was changed) it would rerender the template. This isn't happening in ...

I am building an image-upload feature into a web application and am using the following event handler: $('input:file').change(function(){ //handle input change }); I have discovered that the ...

I am building an image-upload feature into a web application and am using the following event handler: $('input:file').change(function(){ //handle input change }); I have discovered that the ...

Im using Jquery Isotope i have one item width greater than other div. All items align well but the items are not aligning properly under large width element. http://jsfiddle.net/S5vAG/1381/ <...

Im using Jquery Isotope i have one item width greater than other div. All items align well but the items are not aligning properly under large width element. http://jsfiddle.net/S5vAG/1381/ <...