This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript - document.write error?
I want to write html into javascript.
I am sure this question, has been asked ofter, I know its simple but I can't figure it out.
I tried this:
<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
</script>
But it removes all the other html.
I know this is noob question, I pretty unexperienced at javascript...
document.body.innerHTML += '<h1>This is a heading</h1>';
document.body.innerHTML += '<p>This is a paragraph</p>';
Lets say i have a div on my page and i want to add content to it:
<div id="myDiv"><div>
I can use javascript to do so:
<script>
var div = document.getElementById("myDiv");
var myHtmlString = "<h1>This is a heading</h1>";
myHtmlString += "<p>This is a paragraph</p>"
div.innerHTML = myHtmlString;
</script>
Related
This question already has answers here:
Change tag using javascript [duplicate]
(6 answers)
Closed 2 years ago.
In JavaScript, I would like to simply change the
<section class="row"> to <div class="row">
Preferably in the least amount of code as possible to keep it simple :) I was using document.querySelector but that is as far as I got. Thank you for your help!!
You could try
const newDiv = document.createElement('div');
const oldSection = document.querySelector('section.row');
newDiv.innerHTML = oldSection.innerHTML;
newDiv.className = 'row';
oldSection.replaceWith(newDiv);
This question already has answers here:
Creating multiline strings in JavaScript
(43 answers)
Closed 7 years ago.
I need to generate 185 lines of html on a web page when the user clicks on a button and I want to declare the html code as a multiple lines on a variable but I 've problems with the html code on a variable, I tried several ways researching on the web but I couldn't achieve it.
For example:
<script type="javascript">
//it doesn't work
var html = " <li> <!-- 185 lines of html --> </li>";
</script>
Using Heredoc (I thought heredoc notation doesn't work on Javascript -???-) notation seems that works but the javascript contained on the html shows an error.
I really appreciate any help.
You can do something like this:
document.getElementById('button').onclick = function(){
var eleContainer = document.getElementById('container'), //The parent of the of the elements
html = '', //Will be filled with html lines
repeats = 128; //You can change it as much as you want
for(var i=0; i<repeats; i++){
html += '<li></li>';
}
eleContainer.innerHTML = html;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for Javascript (or jquery) code that can automatic replace :
</body>
by (for example ) :
<div>example text</div></body>
Any help would be appreciated.
Why do you want to replace </body>? Instead of replacing, it's a better idea to append the div tag to the body tag, using the .append() method:
$('body').append('<div>example text</div>');
Try using google first.
You can use jquery 'append' function to do this.
Read more here. http://api.jquery.com/append/
Create the div tag with id in your html.
<body>
------------
<div id="sampleId"></div>
-------------
</body>
Use the following in your script.
var div = document.getElementById('sampleId');
div.innerHTML = 'example text';
Just add that element dynamically:
<script type="text/javascript">
function test(){
var div = document.createElement("div");
div.setAttribute("id", "mydiv");
div.className = "notice";
div.style.display = "block";
div.innerHTML = div.innerHTML + 'Extra stuff';
// test case, append the div to the body
document.body.appendChild(div);
}
</script>
If u want to replace something in your page u can use this:
var page = $('html').html();
page = page.replace('</body>', '<div>Test</div></body>');
$('html').html(page);
But if you want just add div to the body element use the code append(''); as suggested ProgramFOX
This question already has answers here:
Change content of a div using JavaScript
(4 answers)
Closed 9 years ago.
document.getElementById("txtOutput").value = result;
instead of using .value=result, can I write something else to say, rewrite to a specific div or paragraph?
If you want to write something (including html-formatations) you can use innerHTML orherwise if you need only the text use innerText:
innerHTML
document.getElementById("txtOutput").innerHTML = result
innerText
document.getElementById("txtOutput").innerText = result;
Yes you can. Look at this simple JsFiddle for an example.
HTML:
<div id="me">
<span>old stuff</span>
</div>
Script
document.getElementById('me').innerHTML = "new stuff";
you can use innerHTML:
document.getElementById("txtOutput").innerHTML = 'some content';
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inserting an element
How to do insert After() in JavaScript without using a library?
I have this little bit of javascript in a project which adds a chat window after the body of the page, I'd like to change it so that it adds the chat window AFTER my specified div "myPublisherDiv". I think this is pretty simple, any ideas? Thanks in advance!
var div = document.createElement('div');
div.setAttribute('id', 'stream' + streams[i].streamId);
document.body.appendChild(div);
This is a bit trickier. insertBefore is simple, for inserting after a node I wrote a litte helper function:
function insertAfter(newElement, referenceElement) {
referenceElement.parentNode.insertBefore(newElement, referenceElement.nextSibling);
}
var div = document.createElement('div');
...
insertAfter(document.getElementById("myPublisherDiv"), div);
But see also https://stackoverflow.com/a/1559632/1048572
This should do it..
var div = document.createElement('div');
div.setAttribute('id', 'stream' + streams[i].streamId);
var target = document.getElementById('myPublisherDiv');
target.parentNode.insertBefore(div, target.nextSibling);
ripped from How to do insert After() in JavaScript without using a library?
Put an id to your div
<div id="myDiv">..</div>
And then append things to it with the appendChild:
document.getElementById("myDiv").appendChild(div);