Move an HTML element down with jQuery
I have 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> below below <div id="seven"></div> . Should I use insertAfter() or after() or is any jQuery function better to fulfill this requirement?
thanks
+4
4 answers
This is the best I could after a little reading ... I needed something more general to use on the site:
BROWSER TESTED: Chrome, Firefox, IE7, IE8, IE9
PURPOSE ... move "Other" to the end ... Server-side code has not changed 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> 0