Solved

jQuery: Appending content REMOVES previously appended content

Posted on 2011-09-04
2
361 Views
Last Modified: 2012-05-12
There is a bug with my code.  If you add an item and then make a selection, content is appended.  The problem is that if you add another item and make another selection, the content which was previously appended is removed.  No content should be removed, even when additional content is added.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
        <title>
            Demo
        </title>
        <style type="text/css">
        div.item {
        background:#eee;
        display:block;
        margin:10px;
        padding:10px 10px 5px;
        }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    </head>
    <body>
        <h1>If you add an item and select an option, content added by previous selections should NOT be removed.</h1>
        <div id="items">
            <div class="item" id="first">
                <p>
                    <label for="serviceItem1">
                        Item:
                    </label>
                    <select id="serviceItem1" name="serviceItem1" class="selectService">
                        <option value="">
                            --- Please Select ---
                        </option>
                        <option value="a">
                            a
                        </option>
                        <option value="b">
                            b
                        </option>
                        <option value="c">
                            c
                        </option>
                    </select>
                </p>
            </div>
        </div>
        <p>
            <input id="addItem" type="button" value="Add Item" />
        </p>
        <div id="itemCage">
            <!-- <div class="a serviceStuff">
            <p>a</p>
            </div>
            -->
            <!-- <div class="b serviceStuff">
            <address>b</address>
            </div>
            -->
            <!-- <div class="c serviceStuff">
            <p>Item C</p>
            </div>
            -->
        </div>
        <script type="text/javascript">
        /* <![CDATA[ */
            $(document).ready(function() {

                $('input').attr('checked', false);
                $('select.selectService').val('');

                selectService();

                var ci = 1;
                var u = $('#first').html();

                $('#addItem').click(function() {
                    ci++;
                    var cii = 1;
                    while ($('#items').html().indexOf('Item' + cii) !== -1 && cii < 9999) cii++;
                    $('#items').append('<div class="item" id="added' + ci + '">' + u.replace(/Item1/g, "Item" + cii) + '</div>');
                    $('#added' + ci).hide();
                    $('#added' + ci + ' span').hide()
                    $('#added' + ci).slideDown(400, function() {
                        $(this).css('height', 'auto')
                    });
                    selectService();
                    if ($('div.item').size() > 4) {
                        $('#addItem').css('display', 'none');
                        $('.later').css('color', '#fa0000')
                    }
                });
            });

            function selectService() {
                $('select.selectService').unbind('change');
                $('select.selectService').change(function() {
                    $('.serviceStuff').remove();
                    showComments(this, $(this).val());
                });
            }

            function showComments(t, _class) {
                var r = new RegExp('<([^\\s]+)[^>]*class="' + _class + '([\\S\\s]*?)</\\1>', 'g');
                var uncommented = $("#itemCage").html().match(r);
                if (uncommented) {
                    var ci = String($(t).parent().find('label').first().attr('for').substr(11));
                    $(t).parent().append(String(uncommented[0].replace('Item1', 'Item' + ci)));
                }
            }
        /* ]]> */
        </script>
    </body>
</html>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
crysallus earned 500 total points
ID: 36481807
I simply commented out the

$('.serviceStuff').remove();

Open in new window

line in selectService and it seemed to behave correctly. Is there a reason that line was in there?
0
 
LVL 16

Author Comment

by:hankknight
ID: 36481812
Hmm, that was a relic from before the code was modified.  You are right, it should be removed.  Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question