Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
How to add interactive modal dynamically into html with Javascript
let http = new XMLHttpRequest();

http.open('get', 'recipe.json', true);

http.send();



http.onload = function () {

if (this.readyState == 4 && this.status == 200) {

    let products = JSON.parse(this.responseText);


    let output = "";


    for (let item of products) {
        output += `

            <div class="product">
                <img src="${item.imageURL}" alt="${item.imageURL}">
                <p class="title">${item.name}</p>
                </p>
                <button type="button" name="${item.name}" class="btn btn-primary modaltrigger" id="btn${item.name}" data-bs-toggle="modal" data-bs-target="#${item.name}">
                Get Recipe
                </button>
            </div>
            <div class="modal fade" id="${item.name}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body">
                        ${item.data}
                    </div>
                </div>
                </div>
                <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>              
        `;

    }

    document.querySelector(".products").innerHTML = output;
}
}

$(document).ready(function () {
    $(".modaltrigger").click(function () {
    var id = $(this).attr('name');
    $('#' + id).modal();
});
});

I want to make a dynamic modal for all the products in my json file with help of javascript. When i click on "Get recipe" Button the modal doesn't pops up and i get an error "ARIA hidden element must not contain focusable elements" in Dev tools.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are not calling your modal function with your modals id.

$("#myModal").modal();

Jquery selector must match modal elements id to modal to show up. And you dont have to create a modal clone for each product you can just change contents of the modal.This will speed up your page loading time.

 let http = new XMLHttpRequest();

http.open('get', 'recipe.json', true);

http.send();



http.onload = function () {

if (this.readyState == 4 && this.status == 200) {

    let products = JSON.parse(this.responseText);


    let output = "";


    for (let item of products) {
        output += `
            <div class="product">
                <img src="${item.imageURL}" alt="${item.imageURL}">
                <p class="title">${item.name}</p>
                </p>
                <button type="button" name="${item.name}" class="btn btn-primary modaltrigger" data-bs-toggle="modal" data-bs-target="#${item.name}">
                Get Recipe
                </button>
            </div>
            <div class="modal fade" id="${item.name}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    </div>
                    <div class="modal-body">
                        ${item.data}
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
                </div>
            </div>              
        `;
    }

    document.querySelector(".products").innerHTML = output;
}
}

$(document).ready(function () {
    $(".modaltrigger").click(function () {
    var id = $(this).attr('name');
    $('#'+id).modal();
});
});

Try this.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda