How to get the `html` element in Javascript? [duplicate] - javascript

This question already has answers here:
How to get the <html> tag HTML with JavaScript / jQuery?
(6 answers)
Closed 4 years ago.
I want to get the html element in Javascript. I can now use document.body.parentNode or document.getElementsByTagName("html"). But I think there should be a simple, "right" way to do it.
So, what's the right way to get the html element in Javascript?
I tried to use search engines, but I don't know how to tell them the html tag is different from any other HTML tag, so it yielded no result I wanted.

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

Chek this answer
https://stackoverflow.com/a/22873490/3134112
reference:
https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement.

I'd say document.getElementsByTagName, querySelector etc. are all "right ways" to get reference to html tag, it doesn't get simpler than that.

Related

Get element by data-i18n-key [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 days ago.
I'm trying to get an element which is added by an external application. The only way I can get this specific element is by the data-i18n-key attribute which I thought I can grab like any data attribute so something like this.
The code:
const buttons = document.querySelectorAll('[data-i18n-key="sdk.seamless_product_reward.paid_items_required"]');
console.log(buttons.length);
<span class="lion-reward-item__redeem-button-text lion-loyalty-page-reward-item__redeem-button-text" data-i18n-key="sdk.seamless_product_reward.paid_items_required">Paid items required</span>
However, this doesn't return anything. Any ideas how to do this?
Of course, Barmer is absolutely right. Your code works. The problem will be that your JS is initialised before the DOM has finished loading. Pack your JS above the closing body tag.

Display HTML code to plain text [duplicate]

This question already has answers here:
How to add plain text code in a webpage? [duplicate]
(11 answers)
Closed 5 years ago.
I need help with including HTML code as plain text on my page. Could you tell me the best way how to place the code on my page between <section> & </section> tags?
Something like this:
Thanks in advance!
So I want to place the HTML code like the w3schools posted this HTML Example on their page with the all tags and tags are colored.
$(selector).text( ... )
The use of the text() function sets the value as a TextNode, which will cause the html not to render on the page.
< code>< /code> Perhaps with this tag you can do it
<code><h1>Mi header</h1></code>
You can use <pre></pre>
For example:
This is me code

Is there any way to add non-element-contained text to a $("")? [duplicate]

This question already has answers here:
How to append/prepend/create a text node with jQuery
(3 answers)
Closed 6 years ago.
I'm creating a JavaScript framework to build HTML documents. First a virtual document is built using jQuery. Right now, I'm experimenting with jQuery's "add" function like so:
$(target).append($("").add($("<div>")).add($("<span>")))
The framework concatenates these calls to build the virtual document before it is appended to the target - this simplified code sample isn't literally what I'm doing. The reason for adding the first $("") is because the framework starts by creating an empty jQuery selection then adds stuff to it. Sub-documents are recursively created and added to parent elements.
This works fine for concatenating elements together, but what if I want to concatenate text? Let's say I want to have something like this rendered:
<div></div> Outside the box!
I can't just do $("<div>").add("Outside the box!") Also, $.after() doesn't seem to work unless the <div> is already on the DOM.
Is this functionality supported by jQuery? If not, are there any workarounds?
Yes, you can use simple string concatenation with current HTML of element: $('<div>').html($('<div>').html() + 'Outside the box!')
Since your code DOM is in memory and not actual HTML, you need to use multi-line code:
var $div = $('<div>', {html: $('<div>')});
$div.html($div.html() + "Outside the box!");

Jquery label value [duplicate]

This question already has answers here:
How to change label text?
(3 answers)
Closed 8 years ago.
I'm trying to set the value of label using Jquery. I've tried .HTML(), .Val(), and .innerHTML but nothing seems to work. Any help would be appreciated.
JavaScript
$("#txtObjective").val(responseData[0].objective);
HTML
<label class="puma_Label" id="txtObjective"></label>
Use textContent (.text() in jQuery). Only input elements have a value property.
$("#txtObjective").text(responseData[0].objective);
Mandatory vanilla explanation:
document.getElementById("txtObjective").textContent = responseData[0].objective;
As for the html options -- those are simply bad practice to use here. Unless you're rendering HTML, never use innerHTML or html().

How to get HTML element similar to getting BODY with document.body? [duplicate]

This question already has answers here:
How to get the entire document HTML as a string?
(16 answers)
Closed 5 years ago.
I know there's a way to do this, but I cannot recall how.
How do I go about grabbing the HTML element (top-most element in the DOM), using plain JavaScript?
Use document.documentElement.
See the docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
var html = document.getElementsByTagName('html')[0];

Categories