How to replace innerHTML of a div using jQuery?
Solution 1:
$("#regTitle").html("Hello World");
Solution 2:
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:
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:
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:
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'
.