Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
- <script>
- document.addEventListener('DOMContentLoaded', () => {
- let data = [{"id": 1, "name": "Иван Иванов", "email": "[email protected]", "phone": "12345344", "title": "Мениджър"},
- {"id": 2, "name": "Мария Петрова", "email": "[email protected]", "phone": "368938736", "title": "Програмист"},
- {"id": 3, "name": "Стефан Колев", "email": "[email protected]", "phone": "866893", "title": "Финансов директор"},
- {"id": 4, "name": "Петя Илиева", "email": "[email protected]", "phone": "10237129", "title": "Бодигард"},
- {"id": 5, "name": "Деница Филева", "email": "[email protected]", "phone": "0739483", "title": "Офис сътрудник"}];
- let select = document.querySelectorAll('.form-select')[0];
- let alert = document.createElement('div');
- console.log(data[0]);
- select.addEventListener('change', (e) => {
- let id = e.target.value;
- let user = null;
- data.forEach((item, index) => {
- if(item.id == id){
- user = index;
- }
- });
- if(user != null){
- document.querySelectorAll('.email-field')[0].value = data[user].email;
- document.querySelectorAll('.phone-field')[0].value = data[user].phone;
- document.querySelectorAll('.title-field')[0].value = data[user].title;
- alert.className = 'alert alert-success';
- alert.innerHTML = 'Намерен потребител!';
- } else {
- alert.className = 'alert alert-danger';
- alert.innerHTML = 'Не е намерен потребител!';
- // [...document.querySelectorAll('[type=text]')].map(item => item.value = '');
- let fields = document.querySelectorAll('[type=text]');
- fields.forEach(item => {
- item.value = '';
- });
- }
- document.querySelectorAll('.col-6')[0].insertBefore(alert, document.querySelectorAll('.col-6')[0].firstElementChild);
- });
- });
- </script>
- </head>
- <body>
- <div class="container">
- <div class="row flex-column ">
- <div class="col-6 mx-auto mt-3">
- <div class="form-group">
- <select class="form-select">
- <option selected="">Избери потребител</option>
- <option value="1">Иван Иванов</option>
- <option value="2">Мария Петрова</option>
- <option value="3">Стефан Колев</option>
- <option value="4">Петя Илиева</option>
- <option value="5">Деница Филева</option>
- <option value="7">Красимир Жечев</option>
- <option value="8">Здравко Стоянов</option>
- <option value="10">Катерина Момчилова</option>
- <option value="11">Панчо Панчев</option>
- <option value="15">Елица Стоинева</option>
- </select>
- </div>
- <div class="form-group">
- <label for="">Имейл</label>
- <input type="text" class="form-control email-field">
- </div>
- <div class="form-group">
- <label for="">Телефон</label>
- <input type="text" class="form-control phone-field">
- </div>
- <div class="form-group">
- <label for="">Длъжност</label>
- <input type="text" class="form-control title-field">
- </div>
- <div class="form-group d-grid">
- <input type="button" class="btn btn-primary" value="Съхрани">
- </div>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment