How to replace innerHTML of a div using jQuery?

JavascriptJqueryInnerhtml

Javascript Problem Overview


How could I achieve the following:

document.all.regTitle.innerHTML = 'Hello World';

Using jQuery where regTitle is my div id?

Javascript Solutions


Solution 1 - Javascript

$("#regTitle").html("Hello World");

Solution 2 - Javascript

The html() function can take strings of HTML, and will effectively modify the .innerHTML property.

$('#regTitle').html('Hello World');

However, the text() function will change the (text) value of the specified element, but keep the html structure.

$('#regTitle').text('Hello world'); 

Solution 3 - Javascript

If you instead have a jQuery object you want to render instead of the existing content: Then just reset the content and append the new.

var itemtoReplaceContentOf = $('#regTitle');
itemtoReplaceContentOf.html('');
newcontent.appendTo(itemtoReplaceContentOf);

Or:

$('#regTitle').empty().append(newcontent);

Solution 4 - Javascript

Here is your answer:

//This is the setter of the innerHTML property in jQuery
$('#regTitle').html('Hello World');

//This is the getter of the innerHTML property in jQuery
var helloWorld = $('#regTitle').html();

Solution 5 - Javascript

Answer:

$("#regTitle").html('Hello World');

Explanation:

$ is equivalent to jQuery. Both represent the same object in the jQuery library. The "#regTitle" inside the parenthesis is called the selector which is used by the jQuery library to identify which element(s) of the html DOM (Document Object Model) you want to apply code to. The # before regTitle is telling jQuery that regTitle is the id of an element inside the DOM.

From there, the dot notation is used to call the html function which replaces the inner html with whatever parameter you place in-between the parenthesis, which in this case is 'Hello World'.

Solution 6 - Javascript

There are already answers which give how to change Inner HTML of element.

But I would suggest, you should use some animation like Fade Out/ Fade In to change HTML which gives good effect of changed HTML rather instantly changing inner HTML.

Use animation to change Inner HTML

$('#regTitle').fadeOut(500, function() {
    $(this).html('Hello World!').fadeIn(500);
});

If you have many functions which need this, then you can call common function which changes inner Html.

function changeInnerHtml(elementPath, newText){
    $(elementPath).fadeOut(500, function() {
        $(this).html(newText).fadeIn(500);
    });
}

Solution 7 - Javascript

jQuery's .html() can be used for setting and getting the contents of matched non empty elements (innerHTML).

var contents = $(element).html();
$(element).html("insert content into element");

Solution 8 - Javascript

you can use either html or text function in jquery to achieve it

$("#regTitle").html("hello world");

OR

$("#regTitle").text("hello world");

Solution 9 - Javascript

Example

$( document ).ready(function() {
  $('.msg').html('hello world');
});

    <!DOCTYPE html>
    <html>
      <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>    
      </head>
      <body>
        <div class="msg"></div>
      </body>
    </html>

Solution 10 - Javascript

$("#regTitle")[0].innerHTML = 'Hello World';

Solution 11 - Javascript

Just to add some performance insights.

A few years ago I had a project, where we had issues trying to set a large HTML / Text to various HTML elements.

It appeared, that "recreating" the element and injecting it to the DOM was way faster than any of the suggested methods to update the DOM content.

So something like:

var text = "very big content";
$("#regTitle").remove();
$("<div id='regTitle'>" + text + "</div>").appendTo("body");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Should get you a better performance. I haven't recently tried to measure that (browsers should be clever these days), but if you're looking for performance it may help.

The downside is that you will have more work to keep the DOM and the references in your scripts pointing to the right object.

Solution 12 - Javascript

jQuery has few functions which work with text, if you use text() one, it will do the job for you:

$("#regTitle").text("Hello World");

Also, you can use html() instead, if you have any html tag...

Solution 13 - Javascript

Pure JS and Shortest

Pure JS

regTitle.innerHTML = 'Hello World'

regTitle.innerHTML = 'Hello World';

<div id="regTitle"></div>

Shortest

$(regTitle).html('Hello World'); 

// note: no quotes around regTitle
$(regTitle).html('Hello World'); 

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="regTitle"></div>

Solution 14 - Javascript

var abc = document.getElementById("regTitle");
abc.innerHTML = "Hello World";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontonyfView Question on Stackoverflow
Solution 1 - JavascriptZedView Answer on Stackoverflow
Solution 2 - JavascriptzombatView Answer on Stackoverflow
Solution 3 - JavascriptCineView Answer on Stackoverflow
Solution 4 - JavascriptdaneView Answer on Stackoverflow
Solution 5 - JavascriptWebengView Answer on Stackoverflow
Solution 6 - JavascriptSomnath MulukView Answer on Stackoverflow
Solution 7 - JavascriptPratikView Answer on Stackoverflow
Solution 8 - Javascriptjitendra varshneyView Answer on Stackoverflow
Solution 9 - JavascriptNikit BarochiyaView Answer on Stackoverflow
Solution 10 - JavascriptAl.UrBasebelon TomehView Answer on Stackoverflow
Solution 11 - JavascriptPavel DonchevView Answer on Stackoverflow
Solution 12 - JavascriptAlirezaView Answer on Stackoverflow
Solution 13 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 14 - Javascriptaryan vishwakarmaView Answer on Stackoverflow