Skip to main content

Document contents disappear after clicking "cancel" button on modal

I am building a project using HTML, bootstrap CSS and Jquery. This is my HTML code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="node_modules/bootstrap-social/bootstrap-social.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Exo&display=swap">
    <link rel="stylesheet" href="styles.css">

    <title>TimeGuard</title>
</head>

<body>
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-body">
                    <form>
                        <div class="form-group row">
                            <label for="name" class="col-form-label">NAME: <input id="targetname" type="text"
                                    placeholder="Enter name of target website" style="width:350px"></label>
                            <label for="url" class="col-form-label">URL: <input id="targeturl" type="text"
                                    placeholder="Enter URL of target website"
                                    style="width:350px;margin-left:15px;"></label>
                            <div class="alert alert-info" role="alert">
                                Both the input fields are mandatory
                            </div>
                            <div>
                                <button type="submit" class="btn btn-light" id="close">Cancel</button>
                                <button type="submit" class="btn btn-dark" id="submit"
                                    disabled="disabled">Submit</button>
                            </div>

                        </div>
                    </form>
                </div>
            </div>

        </div>

    </div>

    <div class="main">
        <div>
            <nav class="navbar">
                <ul class="nav nav-tabs nav-justified" id="firstul">
                    <li class="nav-item active" style="width:200px"><a class="nav-link" href="#dashboard">DASHBOARD</a>
                    </li>
                    <li class="nav-item inactive" style="width:200px;"><a class="nav-link" href="#reports">REPORTS</a>
                    </li>
                    <li class="nav-item inactive" style="width:500px;"><a class="nav-link" href="#addict">ADDICT
                            SETTINGS</a></li>
                </ul>
            </nav>
        </div>

        <div class="tab-content" style="padding:20px;">
            <div class="tab-pane fade active show" role="tab-panel" id="dashboard">
                <div>
                    <h1>OVERVIEW</h1>
                    <div class="container-fluid box">
                        <div class="row justify-content-around">
                            <div class="overview col-3">
                                <div style="font-size:79px;">
                                    00:00:00
                                </div>
                            </div>
                            <div class="overview col-3">
                                <div style="font-size:79px;">
                                    00:00:00
                                </div>
                            </div>
                        </div>
                        <div class="row justify-content-around">
                            <div class="col-3">

                            </div>
                            <div class="col-3">

                            </div>
                        </div>
                    </div>
                </div>
                <div>
                    <h1>TARGET WEBSITES</h1>
                    <div class="box container-fluid">
                        <div class="row justify-content-center">
                            <div id="tarwebs" class="col-6">
                                
                            </div>
                            <div class="row">
                                <i id="addbtn" class="fa fa-plus-circle" type="button"
                                    style="font-size:50px;margin-left:650px;margin-top:20px;"></i>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" role="tab-panel" id="reports">
                    <h1>DAILY</h1>
                    <div class="box container-fluid">
                        <div class="row justify-content-around">

                        </div>
                    </div>
                    <h1>WEEKLY</h1>
                    <div class="box container-fluid">
                        <div class="row justify-content-around">
                            <canvas id="myChart" style="width:100%;max-width:600px"></canvas>
                        </div>
                    </div>
                    <h1>MONTHLY</h1>
                    <div class="box container-fluid">
                        <div class="row justify-content-around">

                        </div>
                    </div>
                </div>

            </div>
        </div>


        <script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
        <script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
        <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
        <script src="dashboard.js"></script>
</body>

</html>

This is CSS code:

body{
    font-family: 'Exo', sans-serif;
}

.main{
    background-color:rgb(4, 209, 4);
    margin:30px;    
    border-radius:10px;
    padding-top: 0px;
    padding-bottom:20px;
}

.navbar{
    background-color:white;
    padding-left:0px;
    padding-right:0px;
    margin-left:auto;
    margin-right:auto;
    padding:0px;
}

.nav-link{
    color:black;
}

li{
    font-size:25px;
    text-align:center;
}

li.active{
    background-color: rgb(4, 209, 4);
    color:black;
    border:5px double;
    border-bottom:none;
}

li.inactive{
    background-color: green;
    
}

.box{
    border:1px solid rgb(92, 86, 86);
    border-radius:10px;
    padding:20px;
}

.overview{
    background-color: white;
    padding:5px;
    justify-content:center;
    border-radius:50px;
    text-align:center;
}

#tarwebs{
    background-color: rgb(128, 183, 231);
    padding:10px;
    justify-content:center;
    border-radius:50px;
}

.targets{
    background-color: white;
    height: 40px;
    border-radius:25px;
    padding:8px 30px 8px 15px;

}

.target-input{
    background-color: antiquewhite;
    border-radius:2px;
}

.fa-star:hover{
    color:rgb(241, 241, 214);
}

.select{
    color:yellow;
}

.deselect{
    color:black;
}

This is the Javascript(Jquery) code:

$(document).ready(function () {
    $("#firstul li").on("click", function () {
        $("#firstul li").removeClass("active").addClass("inactive");
        $(this).removeClass("inactive").addClass("active");
        $(".tab-pane").removeClass("active show").addClass("d-none");
        let value = this.children[0].getAttribute("href");
        $(value).addClass("active show").removeClass("d-none");
    });
    $("#addbtn").on("click", function () {
        $("#myModal").modal("show");
    });
    $("#close").on("click", function () {
        $("#myModal").modal("hide");
    });

    $("#targeturl").on("input change", function () {
        if ($("this").val() == "")
            $("#submit").prop("disabled", true);
        else
            $("#submit").prop("disabled", false);
    });


    $(".fa-star").on("click", function () {
        $(this).toggleClass("select deselect");
    });

    $("#submit").on("click", function (e) {
        e.preventDefault();
        $("#myModal").modal("hide");
        let url = $("#targeturl").val();
        let name = $("#targetname").val();
        let domain = new URL(url);
        domain = domain.hostname;
        if (name == "") {
            name = domain;
        }
        let text = `<div class="targets" style="margin-down:10px;"><img src="https://s2.googleusercontent.com/s2/favicons?domain_url=${domain}"><b style="margin-left:10px;">${name}</b><i class="fa fa-trash" style="font-size:25px;float:right;" type="button"></i><i class="fa fa-star select" style="font-size:25px;float:right;margin-right: 10px;" type="button"></i></div>`;

        document.getElementById("tarwebs").innerHTML+=text;

    })

});

My issues are:

  1. After entering the form specified in the modal and submitting it, the document is changed as expected (A div element appears as expected) . But after opening the modal again (By clicking the plus icon below), if I click "cancel" button without entering any detail , the page reloads and all the previous div elements in the document disappear. I want that the user should retain the previous div element as well as add another div element by filling the modal again.

  2. After re-opening the modal second time, the previous inputs are retained. I have to first delete those inputs before filling another.

I would be glad if someone could solve these issues.

Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

Comments

Popular posts from this blog

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...

Sorting large arrays of big numeric stings

I was solving bigSorting() problem from hackerrank: Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array. I know it works as follows: def bigSorting(unsorted): return sorted(unsorted, key=int) But I didnt guess this approach earlier. Initially I tried below: def bigSorting(unsorted): int_unsorted = [int(i) for i in unsorted] int_sorted = sorted(int_unsorted) return [str(i) for i in int_sorted] However, for some of the test cases, it was showing time limit exceeded. Why is it so? PS: I dont know exactly what those test cases were as hacker rank does not reveal all test cases. source https://stackoverflow.com/questions/73007397/sorting-large-arrays-of-big-numeric-stings

How to load Javascript with imported modules?

I am trying to import modules from tensorflowjs, and below is my code. test.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title </head> <body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script type="module" src="./test.js"></script> </body> </html> test.js import * as tf from "./node_modules/@tensorflow/tfjs"; import {loadGraphModel} from "./node_modules/@tensorflow/tfjs-converter"; const MODEL_URL = './model.json'; const model = await loadGraphModel(MODEL_URL); const cat = document.getElementById('cat'); model.execute(tf.browser.fromPixels(cat)); Besides, I run the server using python -m http.server in my command prompt(Windows 10), and this is the error prompt in the console log of my browser: Failed to loa...