Kamran Ahmed
Kamran Ahmed

Reputation: 12438

Replace all elements with another jquery

I have written this simple JS module that replaces all the elements having a specific data attribute with a dropdown element. For example: If I have this:

<span class="testclass1 testclass2" data-what="itemBox"></span>

It will be replaced by a dropdown. All works fine i.e. conversion from the span element is being done in case there is just a single <span class="testclass1 testclass2" data-what="itemBox"></span> but if I add multiple span elements with data-what="itemBox" only one of these elements get replaced and others don't. Here is the JS Module that I have written:

(function(){

var mould = {

    partyBox        :   $.parseHTML('<select name="mouldedParty"><option value="-1" selected disabled>Select Party</option></select>'),
    iBox            :   $.parseHTML('<select name="mouldedItem"><option value="-1" selected disabled>Select Item</option></select>'),

    itemCSS     :    {
                        'padding'    : '10px',
                        'border'     : '1px solid #ccc',
                        'background' : 'whitesmoke',
                        'color'      : '#2d2d2d',
                        'cursor'     : 'pointer'
                      },        

    partyCSS    :     {
                        'padding'    : '10px',
                        'border'     : '1px solid #ccc',
                        'background' : '#368EE0',
                        'color'      : 'white',
                        'cursor'     : 'pointer'
                      },

    init            :   function (){ },

    process         :   function (container) {
                            mould.mouldParty(container);
                            mould.mouldItems(container);                            
                        },

    mouldParty      :  function(container){
                            var pBox     = $(mould.partyBox);
                            var pBoxes   = $(container).find('[data-what=partyBox]');

                            pBox.css(mould.partyCSS);
                            mould.replaceAllWith(pBoxes, pBox);
                        },

    mouldItems      :  function(container){
                            var iBox     = $(mould.iBox);
                            var iBoxes   = $(container).find('[data-what=itemBox]');

                            iBox.css(mould.itemCSS);
                            mould.replaceAllWith(iBoxes, iBox);
                        },                          

    replaceAllWith  :   function(prevEls, newEl){
                            $(prevEls).each(function(index, elem){
                                $(elem).replaceWith(newEl);
                            });         
                        }
};

mould.process('body');

})();

Can anyone please tell me whats wrong with the code? Why only one element is being replaced while I have written the code to replace all the elements?

JSFiddle Here: http://jsfiddle.net/DK2pe/1/

UPDATE: Added comments to the fiddle http://jsfiddle.net/DK2pe/2/

Upvotes: 0

Views: 91

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$(prevEls).replaceWith(function(idx, el){
    return $(el).clone()
})

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359826

You need to clone newEl for each replacement. As per the replaceWith API documentation:

the selected element replaces the target by being moved from its old location, not by being cloned.

Therefore, change your code roughly like so:

replaceAllWith  :   function(prevEls, newEl){
                        $(prevEls).each(function(index, elem){
                            $(elem).replaceWith(newEl.clone());
                        });         
                    }

Upvotes: 2

Related Questions