views:

1074

answers:

1

I am having some issues with Microsoft Explorer 6/7 and the jQuery "clone" function. The jQuery that I am using is:

$(function() {
    $('#addFields').click(function() {
        var newCredit = $('#original').clone(); // create new set
        newCredit.find('input').val(''); // empty input fields
        $(this).before(newCredit); // append at the end
        return false;
    });
});

The html form looks like this:

<div id="original">

    <li id="prodEnt" >
     <label class="description" for="entity[]">Entity </label>
     <div>
      <input name="entity[]" class="element text medium" type="text" value="" /> 
     </div> 
    </li>  

    <li id="entFunc" >

    <label class="description" for="element_5">Function </label>
    <div>
     <input name="function[]" class="element text medium" type="text" value="" /> 
    </div> 
    </li>

    <li class="section_break_small"></li>
</div>

The Microsoft browser is unable to replicate more than one clone, and does not style (css) the new elements.

Please advise me to a IE friendly alternative! Thanks.

+5  A: 

You can't/shouldn't have <li> elements without a parent <ul>. Furthermore, you can't/shouldn't have <div> elements inside the <li> like you have. Finally, you have id attributes in the HTML that is being cloned, and this will result in duplicate elements with the same id. If you replace the outer original div and make it a <ul>, get rid of the id attributes and make it a class instead, it should be fine.

All that being said, it does work for me on IE7.

EDIT

In response to your comment:

To only select the first one, do this (assuming it has a class of fields):

$('ul.fields').eq(0).clone();
Paolo Bergantino
I have updated the code, and everything works properly, however, when I change the ul that is to be cloned to a class, they replicate exponentially. I have kept the ul that is cloned as an id, and it works in IE, even though it is not standards compliant. I am sure there is a way to have the replicated UL as a class, and only clone one, but I am not sure how to do it. Thank you for your help!
superUntitled
OMG...OMG...OMFFFFFGGG !! Thanks to your answer Paolo, I removed some div's IDs, changed by class=... and now this cloning thingy is working fine in IE... Damned IE! Damned Bill! Damned M$!! Amen! LOL!
Enrique