Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Question:insert innerHTML by condition

  1. #1
    Join Date
    Jul 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    Code:
    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Don't use innerHTML. It has many issues, one of which is that it doesn't work properly with tables in IE.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you for your reply

    what can I use to do that ?

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    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.

    Anyhow, this would work (if that's what you want):

    Code:
    <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.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    innerHTML is not necessarily bad if you know how to use it.
    It is. See http://slayeroffice.com/articles/inn..._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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    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.

  7. #7
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    I find people who use innerHTML to create elements are lazy.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    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, 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.

    Edit: Use for jsonToDom():
    Code:
    myElement.innerHTML += "<p class=\"foo\" name=\"bar\" style=\"baz\"><span id="quux" onclick=\"alert(&quot;Hi!&quot;);\">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.
    Last edited by Twey; 12-30-2007 at 12:44 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    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 wrong while, at the same time, DD-representatives dont't like it, to say the least?

    Arie Molendijk.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •