Get script from a DIV with JS

Asked

Viewed 765 times

2

It is possible to take the SCRIPT from an element.. EX:

<div id="form">
    <div class="box">
        <p>Pegar apartir da 'class="box"'</p>
    </div>
</div>

take the complete code inside the partition id="form"? That would be:

<div class="box">
    <p>Pegar apartir da 'class="box"'</p>
</div>

2 answers

5


If by "script" you mean get all the HTML inside the div, do so:

var form = document.getElementById('form');
var conteudo = form.innerHTML;
alert(conteudo);
<div id="form">
    <div class="box">
        <p>Pegar apartir da 'class="box"'</p>
    </div>
</div>
    

0

As the question is in the "jQuery" category I will give the answer in jQuery:

var div_form = $('#form');
var html = div_form.html();

if you do

console.log(html);

you will see the html code extracted from the #form element.

You can use the simplified version:

var html = $('#form').html();

Browser other questions tagged

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