How to append text to a `div`?
Try this:
var div = document.getElementById('divID');
div.innerHTML += 'Extra stuff';
Javascript : Append text into div?
Try this:
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var day = date.getDate();
var month = date.getMonth();
var yy = date.getFullYear();
var year = (yy < 100) ? yy + 1900 : yy;
var timer = document.createElement('div');
timer.innerHTML = months[month] + " " + day + ", " + year;
document.getElementById('time').appendChild(timer);
Demo here
Adding text to an existing text element in JavaScript via DOM
The reason that appendChild
is not a function is because you're executing it on the textContent
of your p
element.
You instead just need to select the paragraph itself, and then append your new text node to that:
var paragraph = document.getElementById("p");var text = document.createTextNode("This just got added");
paragraph.appendChild(text);
<p id="p">This is some text</p>
Appending large block of html with append()
Remove the line breaks.
http://jsfiddle.net/DmERt/
var large = '<div class="accordian_container"><a href="#" class="accordian_trigger"><h4>Co-Borrower Information</h4></a><hr/><div class="accordian_item" id="accord_item_2"><label> First Name</label><br/><input type="text"/><br/><label>Middle Name</label><br/><input type="text"/><br/><label>Last Name</label><br/><input type="text" /><br/><label>Home Number</label><br/><input type="text"/><br><label>Work Number</label><br/><input type="text"/><br><label>Cell Number</label><br/><input type="text"/><br></div></div>';
$('#accordion_container').append(large);
insert content into div from textarea Javascript
First of all you shouldn't create html elements manually since they would be XSS vulnerable and read about escaping mechanics to prevent malicious code being injected.
Try using document.createElement('div');
method to create div with valid innerText.
later use method:
innerDiv.appendChild(createdElement);
To append element.
You could create builder to build html elements you need and you have to htmlEncode text that will be inside of div element.
const sendButton = document.getElementById('send-btn');const textArea = document.getElementById('input');const innerDiv = document.getElementById('inner');var message = textArea.value;
sendButton.addEventListener('click', function () { const message = new MessageContainerBuilder().BuildMessage(textArea.value); innerDiv.appendChild(message); textArea.value = '';});
function encodeHtmlEntity(input) { var output = input.replace(/[\u00A0-\u9999<>\&]/gim, function (i) { return '' + i.charCodeAt(0) + ';'; });
return output;}
function MessageContainerBuilder() { var createDivElement = function (classTest) { var div = document.createElement('div');
var classAttr = document.createAttribute('class'); classAttr.value = classTest; div.setAttributeNode(classAttr);
return div; };
var createSpanElement = function (value, classTest) { var span = document.createElement('span');
if (classTest) { var classAttr = document.createAttribute('class'); classAttr.value = classTest; span.setAttributeNode(classAttr); }
span.innerText = encodeHtmlEntity(value);
return span; };
this.BuildMessage = function (text) { var divContainer = createDivElement('outgoing'); var messageSpan = createSpanElement(text, 'me');
divContainer.appendChild(messageSpan);
return divContainer; };}
<div id="inner"> <div class="incoming"> <div class="them">Lorem </div> </div> <div class="outgoing"> <div class="me">Lorem ipsum </div> </div></div>
<textarea class="input" id="input" placeholder="Message..."></textarea><button class="waves-effect waves-light" id="send-btn">Send</button>
How to append text to div using ID in javascript?
var writecity = document.createTextNode(city);
document.getElementById("mylist").appendChild(writecity);
This should work.
Basically, you only append other DOM Node (as child) to DOM Nodes. so you use document.createTextNode()
to create node and document.appendChild()
to add it as a child to existing node.
How do I add text to specific div element using jQuery?
Several possible alternatives
Others have provided their answers already but let me give you some more alternatives.
$("span.test:first").text("Welcome");
$("#first span.test").text("Welcome");
$("span.test").first().text("Welcome");
$("span.test").eq(0).text("Welcome");
$("span.test", "#first").text("Welcome");
The second and last one likely being the fastest because they target particular container by ID.
(internal jQuery optimisations may prove me wrong with any future version)
Performance comparison
Here's a JSPerf test that performance compares upper possibilities. As anticipated, the second and last approaches are the fastest of all because element selection is much simplified. I've tried running them on Chrome and you may run them in other browsers if you want to and see differences.
jQuery add text to span within a div
You can use:
$("#tagscloud span").text("Your text here");
The same code will also work for the second case. You could also use:
$("#tagscloud #WebPartCaptionWPQ2").text("Your text here");
Related Topics
How to Refresh Page on Back Button Click
Get the Size of the Screen, Current Web Page and Browser Window
Get Element from Within an Iframe
How to Move an Element into Another Element
Check If an Element Contains a Class in JavaScript
Insert HTML With React Variable Statements (Jsx)
How to Reload Current Page Without Losing Any Form Data
Client Checking File Size Using Html5
How to Prevent Iframe from Redirecting Top-Level Window
Jquery: Simulating a Click on a ≪Input Type="File" /≫ Doesn't Work in Firefox
Encryption in JavaScript and Decryption With PHP
How to Get the Selected Radio Button'S Value
How to Programmatically Set the Value of a Select Box Element Using JavaScript
Why Does This Simple Jsfiddle Not Work