Jump to content

document.innerHTML???


PrateekSaxena

Recommended Posts

How do I get and edit the whole documents innerHTML??I mean should I enclose everything that is in the BODY into a DIV or is there a way to edit the documents HTML??I want to add a new DIV to the body, how do I do that??

Link to comment
Share on other sites

You could first make the div without nothing in it like

<div id="text1">  </div>

And with JavaScript add something in it like

<script type="text/javascript">function makeTxt(){document.getElementById('text1').innerHTML="Your text here";}</script>

OR

<script type="text/javascript">function newDiv(){var page = document.body.innerHTML;document.body.innerHTML=page+" <div id='text1'> Your text here</div>";}</script>

Havn't tested the code's yet , reply if something is wrong.

Link to comment
Share on other sites

I already know that I can do it in this way, and I said so in my initial message. I want to know some other way to do the same!!

Link to comment
Share on other sites

You could change the page content using AJAX if that's what you mean?So if a user clicks a link, then it's sort of like a frame, except not using frames. Does this make sense? I'd read the AJAX tutorial at W3Schools if you haven't already.

Link to comment
Share on other sites

you can use the DOM.if you want to add a new div directly to the body you can do this.

var newDiv = document.createElement("div");newDiv.id = "newDiv";newDiv.appendChild(document.createTextNode("You text here"));document.body.appendChild(newDiv);

Link to comment
Share on other sites

document.createElement("div");
That is what is I was looking for, thanks!!EDIT: by the way aspnetguy, I wanted to tel you that I have posted the code of hanman and nrvtac4 on the so many langs thing!! :) see it
Link to comment
Share on other sites

Thanks! Quite suprisingly I was viewing the same tutorial right now!! :)

Link to comment
Share on other sites

var newDiv = document.createElement("div");newDiv.id = "jsConsole";var it = document.getElementById("jsConsole");

It get the error that it soes not have any properties when I set its height.

Link to comment
Share on other sites

This code works for me in both Firefox and IE:

var newDiv = document.createElement("div");newDiv.id = "jsConsole";newDiv.style.height = "44px";newDiv.style.backgroundColor = "red";

You get the error when you try to set the height? Or are you getting the error when you try to append the element to the document?

Link to comment
Share on other sites

you have not appended the new element to the dom yet. Either add it ot he body or a wrapper of your choice.

var newDiv = document.createElement("div");newDiv.id = "jsConsole";document.body.appendChild(newDiv);var it = document.getElementById("jsConsole");

Link to comment
Share on other sites

you have not appended the new element to the dom yet. Either add it ot he body or a wrapper of your choice.
var newDiv = document.createElement("div");newDiv.id = "jsConsole";document.body.appendChild(newDiv);var it = document.getElementById("jsConsole");

Ahh, right you are, you still have to append the element to the document. But, just so you know, Prateek, there is no reason in doing the document.getElementById("jsConsole") at this point because you already have a reference to that element in "newDiv".
Link to comment
Share on other sites

But, just so you know, Prateek, there is no reason in doing the document.getElementById("jsConsole") at this point because you already have a reference to that element in "newDiv".
ok got ithow come nonw of you have replied to the JS Console in the General Forum??
Link to comment
Share on other sites

Allows you to log stuff and add watched for the time being. More feature coming soon. you can write thing like

con= new myConsole("JS Maker Rel2");con.log("I was logged","info");var ans=42;con.addWatch("The Answer",ans);

more stuff to come...need you guys feedback

Link to comment
Share on other sites

Ok...cool

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...