javascriptjquerywrapall

How to wrap two dynamically created elements with span?


I need to create two elements <img> and <i> and wrap it with div element:

$('<img>', {
  src: 'images/bank-507-logo.png',
  id: "selected_" + selectedBank
})

$('<i>', {
  class: 'fa fa-inr'
})

I have code to wrap a single element like:

$('<img>', {
  src: 'images/bank-507-logo.png',
  id: "selected_" + selectedBank
}).wrapAll('<span/>');

But I need to wrap both <img> and <i> with that span element.


Solution

  • JQuery

    var span= $('<img>', {
            src: 'images/bank-507-logo.png',
            id: "selected_"+"selectedBank"
      }).add($('<i>', {
            class: 'fa fa-inr'
    })).appendTo($('body')).wrapAll($('<span></span>'));
    

    DEMO