Print contents of an array in a tag with id, from a function that is called with an onclick

Asked

Viewed 32 times

-4

Oiii, I am trying to make a function that is called from an onclick and in it take the values of tags, after pricing the button and turning the data into columns of an array, I would like to print this array on a page with tags with specific id.

My problem is that nothing appears that I try to print with . innerHTML

let nome_receita
let ingredientes
let preparo
function enviar(){
    nome_receita = document.getElementById('nome_receita').value;
    ingredientes = document.getElementById('ingredientes').value;
    preparo = document.getElementById('preparo').value;

    if (nome_receita === '' || ingredientes === '' || preparo === ''){
        alert('preencher todos os campos');
    }else{
        document.getElementById('nome_receita').value = '';
        document.getElementById('ingredientes').value = '';
        document.getElementById('preparo').value = '';

        alert('Obrigado por sua contribuição! <3');

        let receita = Array()

        receita.push(nome_receita);
        receita.push(ingredientes);
        receita.push(preparo);
        let v1 = receita[0];
        let v2 = receita[1];**texto em negrito**
        let v3 = receita[2];
        console.log(receita);

        document.getElementById('a').innerHTML = v1;
        document.getElementById('b').innerHTML = v2;
        document.getElementById('c').innerHTML = v3;
        
    }       
}

2 answers

0

Rafael, innerHTML is usually used to execute snippets of HTML code itself.

Example:

document.getElementById('a').innerHTML = '<b>Negrito</b>';

The ideal in these cases is to use innerText instead of innerHTML. That way:

document.getElementById('a').innerText = v1;

Take this test. Since I’m a beginner in the forum, I can’t comment on your question yet, but I have a question, when you run this line:

console.log(receita);

The array is filled in correctly?

-2

Ta missing the operator new:

let receita = Array()

right:

let receita = new Array();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.