javascriptjsfiddlefiddle

Unable to Get this fiddle in page


I am trying to make this fiddle work on web page without success. I have done this before but this fiddle is not working.

I tried to add google's jquery or min jquery, without success. I also tried to add javascript code after div instead of in header, still no success.

can anybody help??

Below is my html code....

<head>
<title>Untitled Document</title>
<script>
$(function() {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function() {
            $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
            i++;
            return false;
    });

    $('#remScnt').live('click', function() { 
            if( i > 2 ) {
                    $(this).parents('p').remove();
                    i--;
            }
            return false;
    });
});
</script>

<style type="text/css">
* { font-family:Arial; }
h2 { padding:0 0 5px 5px; }
h2 a { color: #224f99; }
a { color:#999; text-decoration: none; }
a:hover { color:#802727; }
p { padding:0 0 5px 0; }

input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; }
</style>

</head>

<body>

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
<p>
    <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
</p>
</div>


</form>
</body>

Solution

  • Based on the html you provided, there are few issues.

    1: Jquery is not included in your HTML.

    2: Live function is obsolete. if you have included Jquery version 1.9 or upwards in you web page but missed to mentioned in the question, then you should use event delegation for the dynamically appended elements.

    .on(eventType, selector, function)
    

    Try like below and it should work.

    <html>
    <head>
    <title>Untitled Document</title>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script>
    $(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents p').size() + 1;
    
        $(document).on('click','#addScnt', function() {
                $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
                i++;
                return false;
        });
    
        $(document).on('click','#remScnt', function() { 
                if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
    });
    </script>
    
    <style type="text/css">
    * { font-family:Arial; }
    h2 { padding:0 0 5px 5px; }
    h2 a { color: #224f99; }
    a { color:#999; text-decoration: none; }
    a:hover { color:#802727; }
    p { padding:0 0 5px 0; }
    
    input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; }
    </style>
    
    </head>
    
    <body>
    <form>
    <h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
    
    <div id="p_scents">
    <p>
        <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
    </p>
    </div>
    
    
    </form>
    </body>
    </html>