How could I achieve the following:
document.all.regTitle.innerHTML = 'Hello World';
Using jQuery where regTitle is my div id?
How could I achieve the following:
document.all.regTitle.innerHTML = 'Hello World';
Using jQuery where regTitle is my div id?
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();
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.
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>
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...