innerhtml

M

Mike

Hello,
I keep reading about how NOT to use innerHtml in my Ajax applications,
but no one suggests how to accomplish things without it.I'm looking
for a tutorial, or does someone know of a good book that helps. I use
a lot of php code, so examples with this would be nice.
Thanks
Mike
 
T

Thomas 'PointedEars' Lahn

Randy said:
Mike said the following on 11/24/2007 9:27 PM:

Where? And why?

Good question.
If all you are doing is retrieving some date from the server and display
it on the page, the innerHMTL can't be beat for support.

Yes, it can, for example by using an `input' or `textarea' element. We are
talking about "some data", after all; that is not necessarily HTML code.


PointedEars
 
M

Mike

"Yes, you are correct. People use input and textarea elements for
displaying information in a web page after it has been retrieved
using
AJAX."

I'm looking for examples of not using innerhtml, using something else.
Thanks
Mike
 
M

Mike

Just output from a database query, but without reloading, thats why
I'm using AJAX.I would put the data in a table or textbox.
Thanks,
Mike
 
M

Mike

So any examples NOT using innerhtml?
I'm just looking for the relevant snippets
A. What to send back from the php page
B. What functions to use (NOT innerhtml) to extract the data into my
page.


Thanks
Mike
 
M

Mike

The reason I ask is because ALL the books are using innerhtml and I'm
trying to learn the DOM way, not the javascript way.

Thanks
Mike
 
T

Thomas 'PointedEars' Lahn

Mike said:
The reason I ask is because ALL the books are using innerhtml and I'm
trying to learn the DOM way, not the javascript way.

You have yet to understand first that `innerHTML' is not a part of the
programming language but of the DOM of the UA (a proprietary feature
nevertheless), and ...
[top-posting again]

.... how to quote properly in Usenet.


PointedEars
 
M

Mike

Mike said the following on 11/25/2007 12:25 PM:


And how you do it depends on where it is in a "table" or a "textbox".
One has a .value property, the other one doesn't. And, it depends on the
data itself and what it's intended purpose is. It is a lot easier to
copy/paste from an input element than it is from a "table".


They can't trim signatures and they post upside down.

How can you identify most Google Group posters without viewing the headers?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Thanks?
 
R

RobG

So any examples NOT using innerhtml?
I'm just looking for the relevant snippets
A. What to send back from the php page
B. What functions to use (NOT innerhtml) to extract the data into my
page.

You may as well ask "how do I write an HTML page?".

The basic problem is how you identify what needs to be changed and
then how you change it. You have to consider any attributes that the
replaced content may have and whether you want the new content to
retain those attributes - e.g. style and event handlers.

You also have to consider what the user will do with the content - is
a form (or some form controls), part of a table or an entire table, a
bit of unformatted text or something else?

innerHTML is normally used to completely replace the content of an
element where the replacement has markup in it. Otherwise, DOM
methods can be used. If you are updating the value of an input or
textarea element, you don't need either method - you just update the
element's value.

If you want to add or remove rows or cells from a table, then DOM is
the best way to go. However, the additional feature testing and work-
arounds for browser inconsistencies may be more than some are prepared
to undertake.

It would be much better if you describe what you are trying to do,
then you may get suggestions on solutions using different approaches.
A simple example of replacing the plain text in an element (without
any feature testing or other fallback) is:


<script type="text/javascript">

function updateUsingDOM (id, newText) {
var el = document.getElementById(id);
while (el.firstChild){
el.removeChild(el.firstChild);
}
el.appendChild(document.createTextNode(newText));
}

function updateUsingInnerHTML (id, newText) {
var el = document.getElementById(id);
el.innerHTML = newText;
}

</script>

<form action=""><div>
<input type="text" name="txt">
<input type="button" value="Update using DOM"
onclick="updateUsingDOM('xx', this.form.txt.value);">
<input type="button" value="Update using innerHTML"
onclick="updateUsingInnerHTML('xx', this.form.txt.value);">
</div></form>

<div id="xx"></div>
 
M

Mike

You may as well ask "how do I write an HTML page?".

The basic problem is how you identify what needs to be changed and
then how you change it. You have to consider any attributes that the
replaced content may have and whether you want the new content to
retain those attributes - e.g. style and event handlers.

You also have to consider what the user will do with the content - is
a form (or some form controls), part of a table or an entire table, a
bit of unformatted text or something else?

innerHTML is normally used to completely replace the content of an
element where the replacement has markup in it. Otherwise, DOM
methods can be used. If you are updating the value of an input or
textarea element, you don't need either method - you just update the
element's value.

If you want to add or remove rows or cells from a table, then DOM is
the best way to go. However, the additional feature testing and work-
arounds for browser inconsistencies may be more than some are prepared
to undertake.

It would be much better if you describe what you are trying to do,
then you may get suggestions on solutions using different approaches.
A simple example of replacing the plain text in an element (without
any feature testing or other fallback) is:

<script type="text/javascript">

function updateUsingDOM (id, newText) {
var el = document.getElementById(id);
while (el.firstChild){
el.removeChild(el.firstChild);
}
el.appendChild(document.createTextNode(newText));

}

function updateUsingInnerHTML (id, newText) {
var el = document.getElementById(id);
el.innerHTML = newText;

}

</script>

<form action=""><div>
<input type="text" name="txt">
<input type="button" value="Update using DOM"
onclick="updateUsingDOM('xx', this.form.txt.value);">
<input type="button" value="Update using innerHTML"
onclick="updateUsingInnerHTML('xx', this.form.txt.value);">
</div></form>

<div id="xx"></div>

Rob, Thanks
I think maybe I came across that I didnt know html OR the DOM. Thats
not the case. I was looking for the correct way of sending
back(various, dispariging) information via AJAX to display it
correctly.
I found the answers and I take full responsibility for confusing
everyone.
Thanks
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top