How do i add content in an div with javascript? [duplicate] - javascript

This question already has answers here:
How can I change div content with JavaScript?
(6 answers)
Closed 7 years ago.
How do i change the content of an div on an website that has an id with javascript?
<div class="chat-area" id="chat-area">[Changeable]</div>
I want to know how i add content where it says "[Changeable]" with javascript so i can run the command through the console.
I'd like if you keep your explainage simple, because im still very new to html/css/javascript ;)

In very simple JavaScript terms, you can use:
document.getElementById("chat-area").innerHTML = 'New Content';
And yes, this would work in GreaseMonkey / UserScripts too!

You can use the innerHTML property to achieve your goal like so:
document.getElementById("chat-area").innerHTML = "Your new content here";

Related

How do i add new js code and run on BODY? [duplicate]

This question already has answers here:
Executing <script> elements inserted with .innerHTML
(23 answers)
Closed 2 years ago.
So, I was adding some new js code to body and i notice that if you add with $('body').append(), it works but when i tried to add with document.body.innerHTML+="some script", It doesn't run.
HTML5 specifies that a <script> tag inserted with innerHTML should not execute.
Have a look at Element.innerHTML - Security considerations and the specification.

Using innerHTML has no effect [duplicate]

This question already has answers here:
innerHTML not working javascript
(2 answers)
Closed 7 years ago.
I am trying to use javascript to dynamically change the header to my website and thus the header was not changing. My html, for simplicity, looks like
&ltdiv class="container"&gt
&lth1 id="name"&gtNic&lt/h1&g
&ltp id="email"&gt&lt/p&gt
&lthr&gt
&lt/div&gt
&ltscript src="js/something.js"&gt&lt/script&gt
My javascript lives in a file called something.js and looks like
$(document).ready(function() {
console.log('I got run');
$('#name').innerHTML = "YO";
$('#email').innerHTML = "why hello";
})
For some reason I see the log in the console but the html never changes. Why is the header not getting changed?
I did try looking at the stack overflow question
Javascript: Does not change the div innerHTML
and here Setting innerHTML: Why won't it update the DOM? and many others however none of them address my issue.
In jQuery you use .html("text") instead of .innerHTML
$(document).ready(function() {
console.log('I got run');
$('#name').html("YO");
$('#email').html("why hello");
})
Jquery doesn't have innerHTML property to set html instead it has method.
Use .text() if your content is only plain text
Like this
$('#name').text("YO");
If it has html content then use .html()
Like this
$('#name').html("<p>YO</p>");
DEMO

How to Run two JS functions on one Click? [duplicate]

This question already has answers here:
How to call multiple JavaScript functions in onclick event?
(14 answers)
Closed 7 years ago.
I was trying to run two JS functions in one click using this. Can anybody show me how to do it correctly?
<button onclick="getlocation" onclick="showDiv()">Try It</button>
I tried adding a ; between the two functions but it didnt work too.
"Adding ";" semicolon didn't work, if you could help.."
It works for sure, you have to do it like that: onclick="getLocation();showDiv()"
Was not relevant in this case:
The problem could be, that both functions are setting the content of your div, so the second function will overwrite the content!
Use .innerHTML += "..."; on you second function, this will append the content to the exisitng content.

I want to get the text of an html element and put it in a JavaScript variable [duplicate]

This question already has answers here:
Assign div text to variable then show it
(3 answers)
Closed 8 years ago.
I have the following div:
<div id="mydiv">something</div>
I need to get the content of the div (mydiv) and put it into a JavaScript variable. How can i do it?
var content = document.getElementById("mydiv").innerHTML;

how to visit my site by .xyz.com/#divname, can any one help me? [duplicate]

This question already has answers here:
How to scroll an HTML page to a given anchor
(17 answers)
Closed 8 years ago.
I want browse directly to my site div by this way:
www.xyz.com/#divname
some sites I saw , I know there is a way to do it ,can anyone tell me the code
I have tried
window.location.href=#divname
but not scrolling.
Try
window.location.hash = '#divname';
Where #divname is the id of your div
Can you try this,
<body onload="window.location.hash = '#divname';">
That is called bookmark links, see here http://www.hyperlinkcode.com/bookmark.php.
To navigate to an anchor via javascript you can use this code:
window.location.hash = "#divname";
This topic is also a duplicate of the following:
How to scroll HTML page to given anchor using jQuery or Javascript?
Regards.
If you want to browse directly using a URL (as per your question), then there is absolutely no need for any javascript.
All you need to do is set an id for your div elements, that match the anchor tag you want to use. In the case of your example:
www.xyz.com/#divname
Then you would need an element like so:
<div id="divname">
</div>
Here is a working example

Categories

Resources