JavaScript jQuery is swapping the tags that I insert jquery swap tag,jquery switch tag

I have the following HTML:

<ul>
  <li></li>
  <li></li>
  <li></li>
</ul>

and the following jQuery code:

$('> ul li', body).each(function()
{
  $('</ul><ul>').insertBefore($(this));
});

This code should insert a closing tag followed by an opening tag for an unordered list in front of each list item, but instead it inserts <ul></ul>. These are the wrong way around! Why does jQuery do this and does anyone know how to solve this problem?

Answer:1

jQuery is not about inserting HTML markup somewhere. It is about manipulating the DOM - and to do that, $('</ul><ul>') will somehow get parsed to an element which then gets insertedBefore(this). Since </ul><ul> is invalid markup, your browser does its best when trying to parse it and comes up with <ul/> then, which gets inserted in your document.

Answer:2

I have a datepicker, and I need to be able to enter a date manually in multiple different formats. For example, for May 7, 2015, I need to be able to enter: 05/07/2015 05072015 050715 5715 I ...

I have a datepicker, and I need to be able to enter a date manually in multiple different formats. For example, for May 7, 2015, I need to be able to enter: 05/07/2015 05072015 050715 5715 I ...

I want to send some JS Script from controller to GSP view . I do the following attempt. Controller(Purchase.groovy) : def myaction={ flash.script= 'jQuery("div#header").show(1000);' ...

I want to send some JS Script from controller to GSP view . I do the following attempt. Controller(Purchase.groovy) : def myaction={ flash.script= 'jQuery("div#header").show(1000);' ...

  1. send email javascript code
  2. send mail javascript code
  3. send email using javascript code
  4. send javascript alert from code behind c#
  5. send email through javascript code
  6. javascript send html code
  7. send mail using javascript code
  8. javascript send key code
  9. javascript send error code
  10. node.js send javascript code
  11. send verification code javascript
  12. send email html code javascript
  13. javascript send value to code behind

I am making a chrome extension that will take come content from the current selected tab. I am injecting a script file into the current page and that script communicates with the script from the ...

I am making a chrome extension that will take come content from the current selected tab. I am injecting a script file into the current page and that script communicates with the script from the ...

I have the following code in my javascript. I'd like the alert to happen wnen a user checks the radio button, but instead it happens when the page loads, and NOTHING happens when the radio button ...

I have the following code in my javascript. I'd like the alert to happen wnen a user checks the radio button, but instead it happens when the page loads, and NOTHING happens when the radio button ...