Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    object.innerHTML substitute

    is innerHTML html valid?
    what can be other chance to insert in a specific element group of elements?

    thanks

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    24
    Thanks
    0
    Thanked 4 Times in 4 Posts
    you have to use the
    PHP Code:
    var newElement document.createElement('ELEMENT'); 
    and then do
    PHP Code:
    obj.parentNode.appendChild(newElement); 

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    there are more methods to create elements and attributes and to append elements. The main are:

    document.createElement('tagname'); //to create tagged elements
    document.createTextNode('sometext');//to create text

    root.appendChild(newElement);//to append an element to a root (parent)
    root.insertBefore(newElement,someChild);// to insert an element in a specific place

    newElement.setAttribute('attribute','value');// to set an attribute
    newElement.style.someCSSattribute='value'; // to set a CSS attribute
    newElement.className='someclass'; // to set a class
    newElement.onevent=function(){somefunction(param)}// to attach an event
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    is it worth mentioning that although .innerHTML might not be valid, it's present in all the main browsers,probably isn't going away any time soon and is often easier to use?

  • #5
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay
    is it worth mentioning that although .innerHTML might not be valid, it's present in all the main browsers,probably isn't going away any time soon and is often easier to use?
    Easier perhaps, but I find that DOM is a lot more fun to play with.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by GJay
    is it worth mentioning that although .innerHTML might not be valid, it's present in all the main browsers,probably isn't going away any time soon and is often easier to use?
    innerHTML it is a good weapon, use it carefully, but if you try to use it and later try to access the DOM tree, u might have some problems (AJAX users knows what I mean)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    If you want to have fun, compare render times with Firefox and IE on DOM methods verus innerHTML. Firefox may surprise you.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    yes, I know, innerHTML is faster. This is why sometimes is good to be used, it depends on whether the DOM tree is to be called later or not.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I personally use DOM methods and deal with IE's quirks!
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    so do I. I guess that I use innerHTML only to check/debug the code
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so, generally, innerHTML is, when not calling DOM structure ( i know, the inserted elements does not show there), the easiest way.

    so i will stick in a innerHTML for now.

    only one more thing - how can i insert <script> ? innerHTML does not eval it

    anything else? document.write? those .createElement and .insertText methods?

  • #12
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by looka
    how can i insert <script>
    You can use createElement:
    Code:
    function loadJS(file){
        if(document.createElement){
            var el = document.createElement('script');
            el.setAttribute('src', file);
            document.documentElement.appendChild(el);
        }
    }

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,614
    Thanks
    0
    Thanked 645 Times in 635 Posts
    innerHTML is supported by all the major browsers and was added to the W3C standards a few months ago. It is therefore entirely valid to use it, you just have to be aware that it works slightly differently from the DOM and so may not give the expected results depending on what it is you are trying to do.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #14
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    felgall, could you show us a link for that?
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #15
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    innerHTML it is a good weapon, use it carefully, but if you try to use it and later try to access the DOM tree, u might have some problems (AJAX users knows what I mean)
    What problems are those?

    afaik you will get a DOM tree from an innerHTML operation, as long as the page is served as text/html.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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