Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I convert HTML code into a JavaScript String variable?

I am wondering how I can convert my HTML code that has been garbed with my document.getElementById("landornot") function and convert it into a string. For example right now when I console.log it I get <li class="listing-property-type" id="landornot"><span class="label">Property Type: </span><a href="https://axisutahdev.wpengine.com/property-types/land/" rel="tag">Land</a></li>

What I want is it to be in a string so it should look like this '<li class="listing-property-type" id="landornot"><span class="label">Property Type: </span><a href="https://axisutahdev.wpengine.com/property-types/land/" rel="tag">Land</a></li>'

I haven’t been able to find any helpful resource online any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My Code:

<script>
    window.onload = (event) => {
        const land = document.getElementById("landornot");
        console.log(land);
        
        if (land === '<li class="listing-property-type" id="landornot"><span class="label">Property Type: </span><a href="https://axisutahdev.wpengine.com/property-types/land/" rel="tag">Land</a></li>') {
            console.log('Right Page')
        } else {
            console.log('Wrong Page')
        }
    };
</script>

>Solution :

Have you tried using the .outerHTML property? E.g. console.log(land.outerHTML);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading