Skip to content Skip to sidebar Skip to footer

Jquery Clone Not Functioning As Expected

I am working on a program that is not functioning as I am expecting it. I have a page that allows people to add multiple links to my application, but there is something going on un

Solution 1:

@Mathew Try this

$('.section-group-js').on('keyup', 'input[type="url"]', function() {

        var section = $(this).closest('section');
        if (section.is(':last-child')) {
            var index = section.data('index');
            var newSection = section.clone();

            newSection.find('input').val('');
            newSection.data('index', index + 1);
            newSection.attr('data-index', index + 1);
            
            updateAttributes(newSection, 'title', index);
            updateAttributes(newSection, 'url', index);
            updateAttributes(newSection, 'visible', index);
            
            newSection.insertAfter(section);
        }
    })

    function updateAttributes( newSection, key, index ) {
        newSection.find(`[name="attach[${index}][${key}]"]`).attr('name', `attach[${index+1}][${key}]`);
    }
.modal {
            display: block !Important
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 20;
            right: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
            -webkit-animation-name: fadeIn;
            -webkit-animation-duration: 0.4s;
            animation-name: fadeIn;
            animation-duration: 0.4s
        }

        .assignment-window {
            display: grid;
            position: fixed;
            overflow: hidden;
            padding: 10px;
            padding-bottom: 16px;
            box-sizing: border-box;
            width: 100vw;
            height: 85vh;
            bottom: 0;
            left: 0;
            border-top-left-radius: 40px;
            border-top-right-radius: 40px;
            background-color: white;
            grid-template-rows: auto 1fr;
            grid-template-columns: 0.9fr 2.5fr;
            grid-template-areas: "asstop asstop""assnav asscontent"
        }


        /* ----------[ASS TOP]---------- */

        .asstop {
            grid-area: asstop;
            padding: 24px 20px;
            box-sizing: border-box;
            border-bottom: 2px solid #581F98;
        }

        .asstop .title {
            display: flex;
            align-items: center;
        }

        .asstop .title input {
            flex: 1 1;
            font-size: 24px;
            border-radius: 8px;
            margin-right: 20px;
            border: 1px solid lightgray
        }

        .asstop select {
            outline: none;
            -webkit-appearance: none;
            padding: 12px 16px;
            font-size: 24px;
            box-sizing: border-box;
            border-radius: 8px;
            border: 1px solid lightgray
        }

        .asstop button {
            margin-top: -5px;
        }


        /* ----------[ASS NAV]---------- */

        .assnav {
            grid-area: assnav;
            padding-top: 20px;
            padding-right: 10%;
            border-right: 1px solid lightgray
        }

        .assnav ul {
            margin: 0;
            padding: 0;
            overflow: hidden;
            list-style-type: none
        }

        .assnav li {
            display: block;
            text-decoration: none;
            color: #484848;
            font-size: 20px;
            padding: 14px 20px;
            margin-bottom: 10px;
            border-top-right-radius: 40px;
            border-bottom-right-radius: 40px;
        }

        .assnav li:hover {
            background-color: #F2F2F2
        }

        .assnav li.active {
            background-color: #EEEEEE
        }


        /* ----------[ASS CONTENT]---------- */

        .asscontent {
            grid-area: asscontent;
            display: flex;
            flex-direction: column;
            padding: 30px;
            box-sizing: border-box;
        }

        .asscontent input,
        .asscontent select {
            flex: 1;
            outline: none;
            -webkit-appearance: none;
            padding: 8px 16px;
            font-size: 18px;
            box-sizing: border-box;
            border-radius: 8px;
            border: 1px solid lightgray
        }


        /* ==== Basic Styling ==== */

        .asscontent .basic {
            display: none
        }

        .asscontent .basic textarea {
            flex: 1;
            font-size: 18px;
            border-radius: 8px;
            box-sizing: border-box;
        }

        .asscontent .basic .config {
            display: flex;
            justify-content: space-between;
            padding-top: 20px;
        }

        .asscontent .basic input {
            text-align: center;
        }

        .asscontent .basic .points {
            width: 80px;
        }


        /* ==== Attachment Styling ==== */

        .asscontent .attachments section {
            display: flex;
            justify-content: space-between;
            padding-bottom: 15px;
            margin-bottom: 15px;
            border-bottom: 1px solid lightgray
        }
<head>
    <link rel="stylesheet" href="https://classcolonies.com/resources/style.css">
    <link href="https://fonts.googleapis.com/css2?family=Raleway&display=swap" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <nav></nav>
</head>


<div class="modal">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <div class="assignment-window">
        <div class='asstop'>
            <div class='title'>
                <select>
                    <option>✏️</option>
                    <option>๐Ÿ’ป</option>
                    <option>๐Ÿ“š</option>
                    <option>๐Ÿ’ฏ</option>
                    <option>๐Ÿงช</option>
                </select>
                <input type='text' placeholder='Type assignment title..' value=''>
                <button type="submit" class='button purple-btn'>Save Changes</button>
            </div>
        </div>
        <div class='assnav'>
            <ul>
                <li>Basic Setup</li>
                <li>Attachments</li>
                <li>Advanced Settings</li>
                <li>Delete Assignment</li>
            </ul>
        </div>
        <div class='asscontent'>
            <div class='attachments section-group-js'>
                <section data-index="1">
                    <div class='displayName'>
                        <span> Title: </span>
                        <input name='attach[1][title]' type='text'>
                    </div>
                    <div class='url'>
                        <span>URL: <span>
                                <input name='attach[1][url]' type='url'>
                    </div>
                    <div class='visible'>
                        <span>Visible: <span>
                                <select name='attach[1][visible]'>
                                    <option>- All Students -</option>
                                    <option>๐ŸŸฃ Reading/Social</option>
                                </select>
                    </div>
                </section>
            </div>
        </div>
    </div>
    </form>
</div>
    <?php

    if ( isset($_POST['attach']) ) {
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
        die;
    }
?>

And the output is below

enter image description here


Post a Comment for "Jquery Clone Not Functioning As Expected"