Clone Input Fields

I have a piece of code that I can add unlimited new input rows by clicking an add row button.  This works great unless there is text in the field it's cloning from.  If it does then the text from inside the input fields is also cloned.  How can I prevent this?

here is my jquery & html now:
<script>
                        jQuery(function($){
                            var $button = $('#add-row'),
                                $row = $('.rssRow').clone();

                            $button.click(function(){
                                $row.clone().insertBefore( $button );
                            });
                        });
                    </script>

Open in new window


<form class="col-md-12 add-priv-co" action="submit.php" method="post" style="margin-top:20px;margin-bottom:20px;">

                        <label>Competitor Links</label>

                      
                            <div class="form-group rssRow">
                                <div class="col-md-6 noPadLeft">
                                    <input class="form-control" name="title[]" placeholder="Competitor Name" required/>
                                </div>
                                <div class="col-md-6 noPadLeft">
                                    <input class="form-control facebookURL" name="link[]" placeholder="Link" required/>
                                </div>
                                <div class="clearfix"></div>
                            </div>

               
                        <input type="hidden" name="submitType" value="25"/>
                        <input type="hidden" name="userID" value="<?php echo $userID;?>"/>
                        <input type="hidden" name="orgID" value="<?php echo $orgID;?>"/>

                        <input type="button" class="btn loginBTN pull-right" id="add-row" name="add-row" value="ADD ROW" />
                        <button class="btn loginBTN">SAVE</button>
                        <script>
                            $('.add-priv-co').submit(function () {
                                var setFBurl = "www.facebook.com/";

                                // Get the Login Name value
                                var name = $('.facebookURL').val();

                                // trim it
                                name = $.trim(name);

                                //make it all lowercase
                                name = name.toLowerCase();

                                if (name.indexOf(setFBurl) < 0){
                                    setTimeout(function() { alert('Please enter a correctly formatted Facebook URL.'); }, 100);
                                    return false;
                                }

                            });
                        </script>
                    </form>

Open in new window

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
try:
<script>
                        jQuery(function($){
                            var $button = $('#add-row'),
                                $row = $('.rssRow').clone();
// search for items with class 'form-control' within $row and set their values to empty string
$('input.form-control', $row).val("");

                            $button.click(function(){
                                $row.clone().insertBefore( $button );
                            });
                        });
                    </script>

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nathan RileyFounderAuthor Commented:
Just found a weird bug with this.  If I have 2 input groups then when I add a row it adds 2 not one.

If I have 3 and add a row it adds 3 new groups etc...
0
hieloCommented:
change:
$row = $('.rssRow').clone();

to:
$row = $('.rssRow:eq(0)').clone();
1
Nathan RileyFounderAuthor Commented:
Ah perfect thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.