5

I have a HTML code as below:

<div id="wrap">
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div id="foo"></div>
  <div id="bar"></div>
  <div id="four"></div>
  <div id="five"></div>
  <div id="six"></div>
  <div id="seven"></div>
</div>

I want to move <div id="foo"></div> and <div id="bar"></div> to the bottom under <div id="seven"></div>. Should I use insertAfter() or after() or any jQuery function is better to achieve this requirement?

Thanks

1
  • I would think using the DOM and javascript would do nicely. After all, jQuery uses it. Commented Jan 10, 2012 at 1:59

4 Answers 4

19

appendTo will move the elements to the end of the element's DOM

$('#foo, #bar').appendTo('#wrap');
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you can use .after():

$('#seven').after($('#foo, #bar'));

Comments

1

You can use the following piece of code:

$('#wrap').append('<div id="foo"></div>')

Comments

1

This is the best I could after a little read thru... I needed something a little more generic to be used across the site:

BROWSER TESTED: Chrome, Firefox, IE7, IE8, IE9

OBJECTIVE... move "Other" to bottom... The server side code had no way of changing the markup.

<select name="anyName" id="anyID" >
  <option value="Other">Other</option>
  <option value="Entry 1" >Entry 1</option>
  <option value="Entry 2" >Entry 2</option>
  <option value="Entry 3" >Entry 3</option>
</select>

<script type="text/javascript">
 $(document).ready(function() {
    $("select option").each(function() {
        if($(this).text() === "Other" && $(this).parent("select").children("option:last-child").text() !== "Other") { 
            $(this).insertAfter($(this).parent("select").children("option:last-child"));
        }
    });
 }); 
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.