View Full Version : Question:insert innerHTML by condition
hello all
I have table with one row and 5 columns.
I want to add an innerHtml if the 'td' have specific image
how can I do it?
var _NEW="<table ><tr><td>Hello</td><td rowspan=2>THERE</td></tr><tr><td> Look here</td></tr></table>";
function Addobject()
{
for(var i=1;i<=5;i++)
{
if(what is the condtion should be)
document.getElementById("td_id_"+i).innerHTML=_NEW;
}
}
thanks alot
Don't use innerHTML. It has many issues, one of which is that it doesn't work properly with tables in IE.
thank you for your reply
what can I use to do that ?
molendijk
12-29-2007, 06:38 PM
Hello FDI,
innerHTML is not necessarily bad if you know how to use it. Most of the time, it's much faster than "real" W3C DOM methods, see also this (http://www.quirksmode.org/dom/innerhtml.html).
Anyhow, this would work (if that's what you want):
<html> (give a doctype here)
<head>
<script type="text/javascript">
var _NEW="<table ><tr><td>Hello</td><td rowspan=2>THERE</td></tr><tr><td> Look here</td></tr></table>";
function Addobject()
{
for(var i=1;i<=5;i++)
{ if(document.getElementById('image')){document.getElementById('td_id_').innerHTML=_NEW}
if(document.getElementById('image'+i)){document.getElementById('td_id_'+i).innerHTML=_NEW }
}
}
window.onload=Addobject
</script>
</head>
<body>
<table border=1>
<tr>
<td id="td_id_"><img id="image" src="some.gif"></td>
<td id="td_id_1"><img id="image1" src="some.gif"></td>
<td id="td_id_2"><img id="image2" src="some.gif"></td>
<td id="td_id_3"><img id="image3" src="some.gif"></td>
<td id="td_id_4"><img id="image4" src="some.gif"></td>
</tr>
</table>
</body>
</html>
Give one of the images a wrong ID (for instance: immage2 instead of image2) and you'll see what I mean.
Arie Molendijk.
innerHTML is not necessarily bad if you know how to use it.It is. See http://slayeroffice.com/articles/innerHTML_alternatives/.
Most of the time, it's much faster than "real" W3C DOM methods, see also [a benchmark].Yes; however, it's conceptually very inefficient, meaning that the DOM methods can (and probably will, with the growing emphasis on standards) be optimised to be much more efficient.
molendijk
12-29-2007, 11:41 PM
But innerHTML is so easy! And FF supports innerHTML for XHTML documents. As someone said somewhere: 'I’d rather save myself the time of having to code 20 lines of createElement/appendChild’s when a single innerHTML will suffice'.
That being said, I admit that innerHTML is not part of the W3C DOM. But what about Ajax?
Arie Molendijk.
tech_support
12-30-2007, 12:27 AM
I find people who use innerHTML to create elements are lazy. :p
As someone said somewhere: 'I’d rather save myself the time of having to code 20 lines of createElement/appendChild’s when a single innerHTML will suffice'.It's perfectly simple to write a helper function that makes using the DOM a lot more fun. I usually use something simple and lightweight like:
function map(f, arr) {
for(var i = 0, r = []; i < arr.length; r[i] = f(arr[i++]));
return r;
}
var jsonToDom = (function() {
var TAG = 0, ATTRS = 1, CHILDREN = 2;
function addTo(parent) {
return function(x) {
return parent.appendChild(jsonToDom(x));
};
}
function jsonToDom(o) {
if(typeof o === "string")
return document.createTextNode(o);
var r;
if(o[TAG] instanceof Array) {
r = document.createDocumentFragment();
map(addTo(r), o);
} else {
r = document.createElement(o[TAG]);
for(var x in o[ATTRS])
if(o.hasOwnProperty(x))
r[x] = o[x];
map(addTo(r), o[CHILDREN]);
}
return r;
}
return jsonToDom;
})();... but if you wanted to be really interoperable, you could use JsonML (http://jsonml.org/), which has a sample converter for DOM.
That being said, I admit that innerHTML is not part of the W3C DOM. But what about Ajax?AJAX is a methodology, not a technology, and thus does not require a standard. The technology that makes it possible, the XMLHttpRequest object, is being standardised as I type.
Use for jsonToDom():
myElement.innerHTML += "<p class=\"foo\" name=\"bar\" style=\"baz\"><span id="quux" onclick=\"alert("Hi!");\">Quuux</span>Quuuux</p>";
// becomes:
myElement.appendChild(jsonToDom(["p",
{className: "foo",
name: "bar",
cssString: "baz"},
[["span",
{id: "quux",
onclick: function(e) { alert("Hi!"); }},
["Quuux"]],
"Quuuux"]));
// or, if you've been using innerHTML so long you hate actual code formatting,
myElement.appendChild(jsonToDom(["p", {className: "foo", name: "bar", cssString: "baz"}, [["span", {id: "quux", onclick: function(e) { alert("Hi!"); }}, ["Quuux"]], "Quuuux"]));Slightly more verbose, but much easier to format, no ugly quote escapes, and doesn't erase and recreate every other child in myElement.
jscheuer1
12-30-2007, 12:35 AM
Regardless of how you feel about innerHTML, the fact remains that it will often mess up forms, and that it isn't supported in IE for any table elements.
On the plus side, it is fast and efficient when it will work, and so far as I can tell, virtually essential for many Ajax implementations.
molendijk
12-30-2007, 02:06 AM
May I conclude from the fact that DD offers scripts like the Ajax pagination (http://www.dynamicdrive.com/dynamicindex17/ajaxpaginate/index.htm) script (in which innerHTML is crucial) that the use of innerHTML is not essentially wrong while, at the same time, DD-representatives dont't like it, to say the least?
Arie Molendijk.
May I conclude from the fact that DD offers scripts like the Ajax pagination script (in which innerHTML is crucial) that the use of innerHTML is not essentially wrongHow do you reach that conclusion? Scripts use it therefore it's good? innerHTML is used in that case, but there are better alternatives -- for example, passing JsonML rather than chunks of HTML to the client.
while, at the same time, DD-representatives dont't like it, to say the least?We're volunteers, and not officially related to DD. Of the scripts in the DD archives, I only agree with a few of them. If it were up to me the archives here would be nuked and written from scratch.
so far as I can tell, [innerHTML is] virtually essential for many Ajax implementations.Not at all. No AJAX (Asynchronous Javascript And XML) script will ever require innerHTML. If you look at the deceptively-named "Ajax Pagination" script linked above, you will see:
document.getElementById(divId).innerHTML=page_request.responseTextThe script isn't actually using XML at all, it's just dumping the response text into the element. This is not AJAX.
jscheuer1
12-30-2007, 04:44 AM
I haven't been keeping current on this Json DOM translation stuff. I was under the impression it wasn't fully supported by IE yet.
I'm fairly sure it is coming sooner or later though, even if it isn't here yet.
This business with innerHTML is a bit odd though. Personally, I see nothing wrong in using it where it will be effective. It will most likely never be standard in the strict sense, and therefore never valid in that sense either. It is already a part of the defacto standard of javascript though, so it will continue to be supported for a very long time, though perhaps not in some browsers, in some of the strictest modes.
If a child has a new toy hammer, suddenly the entire world looks like it needs hammering. This is often how scripts get written, relying heavily upon whatever the coder has most recently learned.
I'm as guilty at times of this as the next person. However, of the scripts that I write, the one's that I like best are the ones that draw from all that I know, and that use it appropriately and efficiently.
molendijk
12-30-2007, 07:26 PM
Hello Twey,
You made a couple of things clearer to me. Thanks for the link to the slayeroffice-article: I like it very much.
Arie Molendijk.
I haven't been keeping current on this Json DOM translation stuff. I was under the impression it wasn't fully supported by IE yet.It's just standard Javascript.
Hello FDI,
innerHTML is not necessarily bad if you know how to use it. Most of the time, it's much faster than "real" W3C DOM methods, see also this (http://www.quirksmode.org/dom/innerhtml.html).
Anyhow, this would work (if that's what you want):
<html> (give a doctype here)
<head>
<script type="text/javascript">
var _NEW="<table ><tr><td>Hello</td><td rowspan=2>THERE</td></tr><tr><td> Look here</td></tr></table>";
function Addobject()
{
for(var i=1;i<=5;i++)
{ if(document.getElementById('image')){document.getElementById('td_id_').innerHTML=_NEW}
if(document.getElementById('image'+i)){document.getElementById('td_id_'+i).innerHTML=_NEW }
}
}
window.onload=Addobject
</script>
</head>
<body>
<table border=1>
<tr>
<td id="td_id_"><img id="image" src="some.gif"></td>
<td id="td_id_1"><img id="image1" src="some.gif"></td>
<td id="td_id_2"><img id="image2" src="some.gif"></td>
<td id="td_id_3"><img id="image3" src="some.gif"></td>
<td id="td_id_4"><img id="image4" src="some.gif"></td>
</tr>
</table>
</body>
</html>
Give one of the images a wrong ID (for instance: immage2 instead of image2) and you'll see what I mean.
Arie Molendijk.
hello Arie Molendijk
it's work correctly, thaaaank you
and thanks alot for all
:D
jscheuer1
12-31-2007, 04:48 AM
I haven't been keeping current on this Json DOM translation stuff. I was under the impression it wasn't fully supported by IE yet.
It's just standard Javascript.
And that's supposed to answer this issue, how? For one, XML isn't standard in IE yet. And I've messed around with getting FF, IE and Opera to write out DOM element creation and insertion script code just from valid HTML, so I know a little bit about it. Sure, this is a little different, and someone or some folks may have ironed out the kinks, but in my experience, there are simply some things IE won't do, and several things that are simply done differently in just the three browsers mentioned. I was using only standard javascript at first, and found it unequal to the task.
That experience, and something I seem to remember reading about Json is what prompted me to mention this in the first place.
Yes, I was quite misleading there. It is standard ECMAScript as of version 3 (as is expected, as ECMAScript is the "common factor" of JavaScript, JScript, and various other similar languages).
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.