0

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/

2 Answers 2

2

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());
                        });         
                    }
Sign up to request clarification or add additional context in comments.

Comments

0

Try

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

2 Comments

Why the extra function?
@MattBall I removed the .each() loop

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.