How to get the html tag HTML with JavaScript / jQuery?
The simplest way to get the html
element natively is:
document.documentElement
Here's the reference: https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement.
UPDATE: To then grab the html
element as a string you would do:
document.documentElement.outerHTML
JavaScript/jQuery: how to get HTML and display HTML, including tags
What you need to be able to do is escape the HTML before you output it so that the browser doesn't render the tags contained therein.
You can escape HTML easily using jQuery like this:
var escapedHtml = $('<div />').text($('#html-block').html());
Now you have a string with things like <div id="html-block">
which you can spit out to the browser:
$('#html-block').after($('<pre />').html(escapedHtml));
All in one you could do this:
var $htmlBlock = $('#html-block');
$('<pre />').text($htmlBlock.html()).insertAfter($htmlBlock);
How to get html element tag/text when clicked in Jquery?
I made some changes, used a condition to remove won’t log for span tag, but works as I expected it to.
$(document).ready(function() {
$("body").on('click', function(event) {
var $target = $(event.target);
var getElm = $target.closest('p,div');
if (getElm && getElm.length) {
console.log("I got the element", getElm[0]);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Hello I'm div</div>
<p>I'm para <span>I'm span inside para</span></p>
<p>I'm para 2</p>
How to get the `html` element in Javascript?
You can use:
document.documentElement
which points to the document's root html
node.
https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
How to get the full definition of the HTML tag using Jquery?
One option is to check the element's outerHTML
, and match the <
to the >
:
const openingHtmlTag = $('#myDiv')[0] .outerHTML .match(/<[^>]+>/)[0];console.log(openingHtmlTag);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myDiv" style="color:green" title="foobar">content<div>
How to get all the html inside a tag as it is using jquery?
function decodeEntities(encodedString) { var textArea = document.createElement('textarea'); textArea.innerHTML = encodedString; return textArea.value;}console.log(decodeEntities($('div').first().html()))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div> A& <br> B</div>
Related Topics
Jquery's Live() Is Deprecated. What Do I Use Now
How to Populate the Options of a Select Element in JavaScript
Get Index of Clicked Element Using Pure JavaScript
Downloading Canvas Element to an Image
Copy to Clipboard in Chrome Extension
Find Day Difference Between Two Dates (Excluding Weekend Days)
JavaScript Reload the Page with Hash Value
Switch Statement for String Matching in JavaScript
Socket.Io 1.X: Use Websockets Only
Differencebetween Unary Plus/Number(X) and Parsefloat(X)
Package.JSON' Is Not Under 'Rootdir'
CSS Media Queries and Jquery Window .Width() Do Not Match