Questions tagged [innerhtml]

innerHTML is a DOM node's property that gets or sets the inner HTML code of an HTML element. It is commonly used in Javascript to dynamically change or read from a page.

innerHTML is a DOM node's property that gets or sets the inner HTML code of an HTML element. It is commonly used in JavaScript to dynamically change or read from a page.

Syntax to get innerHTML

var content = element.innerHTML; /* To get the inner HTML of an element */

where,

content contains the serialized HTML code describing all of the element's descendants.

Syntax to set innerHTML

element.innerHTML = content; /* To set the inner HTML of an element */

Removes all of element's children, parses the content string and assigns the resulting nodes as children of the element.

2983 questions
93
votes
5 answers

Advantages of createElement over innerHTML?

In practice, what are the advantages of using createElement over innerHTML? I am asking because I'm convinced that using innerHTML is more efficient in terms of performance and code readability/maintainability but my teammates have settled on using…
oninea
  • 1,423
  • 4
  • 14
  • 15
86
votes
8 answers

JQuery html() vs. innerHTML

Can I completely rely upon jQuery's html() method behaving identical to innerHTML? Is there any difference between innerHTML and jQuery's html() method? If these methods both do the same, can I use jQuery's html() method in place of innerHTML? My…
Bhupi
  • 2,928
  • 6
  • 34
  • 51
77
votes
8 answers

BeautifulSoup innerhtml?

Let's say I have a page with a div. I can easily get that div with soup.find(). Now that I have the result, I'd like to print the WHOLE innerhtml of that div: I mean, I'd need a string with ALL the html tags and text all toegether, exactly like the…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
65
votes
9 answers

Why is "element.innerHTML+=" bad code?

I have been told not to append stuff using element.innerHTML += ... like this: var str = "
hello world
"; var elm = document.getElementById("targetID"); elm.innerHTML += str; //not a good idea? What is wrong with it?, what other…
ajax333221
  • 11,436
  • 16
  • 61
  • 95
61
votes
6 answers

Remove innerHTML from div

I'm trying to clear the div's innerHTML before repopulating it. I tried removeData() but once that's called, when I try to add the data, I get nothing from the next line after remove whereas if I remove the removeData() it's fine again. I just…
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
60
votes
5 answers

JavaScript get TextArea input via .value or .innerHTML?

Is it ok to get the value of a textarea element in JavaScript with myTextArea.value or should I use myTextArea.innerHTML? Thank you.
Francisc
  • 77,430
  • 63
  • 180
  • 276
55
votes
4 answers

How to get the innerHTML of selectable jquery element?

I have a php generated list whose list items are selectable using jquery selectable widget. The list for all intents and purposes is:
  • Item 1
  • Item…
user2489311
52
votes
13 answers

Alternative for innerHTML?

EDIT: WOW. This question is 12 years old now. As someone stated, it can be done with a one-liner since 2016: https://stackoverflow.com/a/69322509/80907 The original: I'm wondering if there's a way to change the text of anything in HTML without…
KdgDev
  • 14,299
  • 46
  • 120
  • 156
50
votes
7 answers

Remove HTML tags from a String in Dart

I’ve been trying to achieve this for a while, I have a string which contains a lot of HTML tags in it which is in some encoded form Like & lt; and & gt; (without the spaces) in between the string. Can anyone assist me in removing those tags so that…
Jaswant Singh
  • 9,900
  • 8
  • 29
  • 50
42
votes
3 answers

Add a "new line" in innerHTML

I am trying to create a table with images in first cell and information about the pic in second cell. I need to add different information in one cell, like that: cellTwo.innerHTML = arr_title[element] + arr_tags[element]; Is it possible to add a…
Gudron Swiss
  • 445
  • 1
  • 4
  • 6
41
votes
4 answers

javascript document.innerHTML set content of whole document

How can I set the innerHTML, or the whole content of an HTML document using javascript? For example my document would look like this:
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
41
votes
3 answers

How do I add a non-breaking whitespace in JavaScript without using innerHTML?

I'm generating content dynamically and in some instances, I need to set a   as the only content of a element. However, the following adds   as text vs adding a empty space: var foo = document.createElement("span") foo =…
frequent
  • 27,643
  • 59
  • 181
  • 333
40
votes
4 answers

How do I clear inner HTML

I've been fiddling with this for a while but it won't work and I can't figure out why. Please help. Here is what I have: lala

Moonkaman227
  • 411
  • 1
  • 4
  • 5

38
votes
8 answers

It says that TypeError: document.getElementById(...) is null

Althought I pushed a parameter to getElementById I wonder from where is this 'is null' error coming from? TypeError: document.getElementById(...) is null [Break On This Error] document.getElementById(elmId).innerHTML = value; Line 75 In…
mcan
  • 1,914
  • 3
  • 32
  • 53
37
votes
11 answers

Remove all content using pure JS

I'm looking for a way to remove the entire content of a web page using pure Javascript -- no libraries. I tried: document.documentElement.innerHTML = "whatever"; but that doesn't work: it replaces the inside of the element. I'm looking at…
Félix Saparelli
  • 8,424
  • 6
  • 52
  • 67