How to convert a jQuery object to html?

Asked

Viewed 131 times

1

Hello, I am trying to create a download button after the page content is deleted. I’m using jQuery to create the button, but I need the html to use as the "Document.write()" parameter. They can transform a jQuery object into an html string?

function criarBotaoDownload(){
    var botao = $("<a>");
    botao.attr("download", "download");
    botao.attr("href", enderecoDoArquivo);
    botao.text("Baixar!");

    //?????

    document.write(//Quero colocar o link aqui!);
}

criarBotaoDownload();

I thought to put inside a div and pull the value with ". innerHTML" but it seemed to me to be a great gambiarra so I came to ask here.

Thanks for your help.

1 answer

1

You really don’t need to do this, let alone use the document.write; just use the function append jQuery:

function criarBotaoDownload(){
    var botao = $("<a>");
    botao.attr("download", "download");
    botao.attr("href", 'url');
    botao.text("Baixar!");

    $('body').append(botao);
}

criarBotaoDownload();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • Hello Anderson, in fact it is not necessary. I just wanted to know if it was possible to turn a jQuery object into html because in this case I would really like to use the document.write() this is possible?

  • @Wiltonribeiro But what justifies this use?

  • I am creating a script to use with browser devtools. This script will look at the code of a particular site, rewrite and then present me the result by deleting the content from the previous site.

Browser other questions tagged

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