-2

How can I convert this whole HTML element:

<div class="g">
    <!--m-->
    <div class="rc" data-hveid="95" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQFQhfKAEwCg">
        <h3 class="r">
            <a href="https://en.wikipedia.org/wiki/Something_(Beatles_song)" onmousedown="return rwt(this,'','','','11','AFQjCNGXYioeoJweF-wB1uZUU1NGNI_A3Q','','0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQFghgMAo','','',event)" target="_blank">
                Something (Beatles song) - Wikipedia
            </a>
        </h3>
        <div class="s">
            <div>
                <div class="f kv _SWb" style="white-space:nowrap">
                    <cite class="_Rm">
                        https://en.wikipedia.org/wiki/<b>Something</b>_(Beatles_song)
                    </cite>
                    <div class="action-menu ab_ctl">
                        <a class="_Fmb ab_button" href="#" id="am-b10" aria-label="Result details" aria-expanded="false" aria-haspopup="true" role="button" jsaction="m.tdd;keydown:m.hbke;keypress:m.mskpe" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQ7B0IYTAK">
                            <span class="mn-dwn-arw"></span>
                        </a>
                        <div class="action-menu-panel ab_dropdown" role="menu" tabindex="-1" jsaction="keydown:m.hdke;mouseover:m.hdhne;mouseout:m.hdhue" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQqR8IYjAK">
                            <ol>
                                <li class="action-menu-item ab_dropdownitem" role="menuitem">
                                    <a class="fl" href="https://webcache.googleusercontent.com/search?q=cache:CRMWFEe8upUJ:https://en.wikipedia.org/wiki/Something_(Beatles_song)+&amp;cd=11&amp;hl=en&amp;ct=clnk&amp;gl=us" onmousedown="return rwt(this,'','','','11','AFQjCNEbZZpTsVP_Rxv695zfNmReizCs7Q','','0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQIAhjMAo','','',event)" target="_blank">
                                        Cached
                                    </a>
                                </li>
                                <li class="action-menu-item ab_dropdownitem" role="menuitem">
                                    <a class="fl" href="/search?newwindow=1&amp;q=related:https://en.wikipedia.org/wiki/Something_(Beatles_song)+something&amp;tbo=1&amp;sa=X&amp;ved=0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQHwhkMAo">
                                        Similar
                                    </a>
                                </li>
                            </ol>
                        </div>
                    </div>
                </div>
                <span class="st">
                    "<em>Something</em>" is a song by the Beatles, written by George Harrison and released on the band's 1969 album Abbey Road. It was also issued on a double A-sided&nbsp;...
                </span>
            </div>
        </div>
    </div>
    <!--n-->
</div>

to a Javascript string?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99

2 Answers2

2

You can use querySelector or similar to get the element, and then outerHTML to get its HTML (including the element's own tags):

alert(document.querySelector(".g").outerHTML);

That assumes that the element you want is the first element in the document with the class g. You'll have to adjust the selector as appropriate, you can use any valid CSS selector. Note that querySelector is also available on individual elements (rather than the document as a whole) if you need to search only part of a document, and there's a form that returns a list of all matches (querySelectorAll) if you need to loop.

Example:

alert(document.querySelector(".g").outerHTML);
<div class="g"><!--m--><div class="rc" data-hveid="95" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQFQhfKAEwCg"><h3 class="r"><a href="https://en.wikipedia.org/wiki/Something_(Beatles_song)" onmousedown="return rwt(this,'','','','11','AFQjCNGXYioeoJweF-wB1uZUU1NGNI_A3Q','','0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQFghgMAo','','',event)" target="_blank">Something (Beatles song) - Wikipedia</a></h3><div class="s"><div><div class="f kv _SWb" style="white-space:nowrap"><cite class="_Rm">https://en.wikipedia.org/wiki/<b>Something</b>_(Beatles_song)</cite><div class="action-menu ab_ctl"><a class="_Fmb ab_button" href="#" id="am-b10" aria-label="Result details" aria-expanded="false" aria-haspopup="true" role="button" jsaction="m.tdd;keydown:m.hbke;keypress:m.mskpe" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQ7B0IYTAK"><span class="mn-dwn-arw"></span></a><div class="action-menu-panel ab_dropdown" role="menu" tabindex="-1" jsaction="keydown:m.hdke;mouseover:m.hdhne;mouseout:m.hdhue" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQqR8IYjAK"><ol><li class="action-menu-item ab_dropdownitem" role="menuitem"><a class="fl" href="https://webcache.googleusercontent.com/search?q=cache:CRMWFEe8upUJ:https://en.wikipedia.org/wiki/Something_(Beatles_song)+&amp;cd=11&amp;hl=en&amp;ct=clnk&amp;gl=us" onmousedown="return rwt(this,'','','','11','AFQjCNEbZZpTsVP_Rxv695zfNmReizCs7Q','','0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQIAhjMAo','','',event)" target="_blank">Cached</a></li><li class="action-menu-item ab_dropdownitem" role="menuitem"><a class="fl" href="/search?newwindow=1&amp;q=related:https://en.wikipedia.org/wiki/Something_(Beatles_song)+something&amp;tbo=1&amp;sa=X&amp;ved=0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQHwhkMAo">Similar</a></li></ol></div></div></div><span class="st">"<em>Something</em>" is a song by the Beatles, written by George Harrison and released on the band's 1969 album Abbey Road. It was also issued on a double A-sided&nbsp;...</span></div></div></div><!--n--></div>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

This may be a way

var str=document.getElementsByClassName('g')[0].innerHTML;

Akhter Al Amin
  • 852
  • 1
  • 11
  • 25