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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    createelement problem in netscape6

    Hi,
    I'm trying to run this code


    var LR = document.createElement(id);
    alert(document.getelementbyid(id));

    the second line is not working. What is the problem ?

    Thank you very much

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Your usage of methods is incorrect for one:

    document.createElement('someString')

    Creates an element with tagName of 'someString':

    For example:

    document.createElement('img')

    Effectively creates a <img/> tag with no set attributes.

    To set any attribute, you should do something like this for example:

    var image = document.createElement('img');
    image.setAttribute('src', 'myimage.gif');
    image.setAttribute('width', '100');
    image.setAttribute('height', '50');
    image.setAttribute('alt', 'alternate text');

    That effectively creates:
    <img src="myimage.gif" width="100" height="50" alt="alternate text"/>

    But it doesn't exist anywhere inside the document, just as an HTMLImageElement object in Javascript. To add this to the document, you will need to make use of some sort of insertion method, the most common being appendChild():

    image = document.body.appendChild(image);

    The image is now the lastChild of the html document's body, and the variable image refers to the live node in the document structure now.

    Kind of make sense?
    (Also going to move this to the DOM Scripting forum, slightly more appropriate...)

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    dJogja id
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if just need rendered outside document, like alert, maybe can ignore name attribute of element (or maybe not ???)
    i tried with this

    ----code
    <a href="javascript:alertme()">alert me</a>
    <script>
    function alertme(){
    var LR = document.createElement('x').y='Hey You!'
    alert(LR)
    }
    </script>
    -----endcode

    I thought:
    "x" is not html tag element and "y" is not atributte element of "x", but for alert no need to append as html tag, tried at IE6 and NN6 working find, also i tried for status-bar, but NN6 can't work...????, anyone can find out this problem ? PLz correct if anything wrong, i learn programming just from book and internet. Thx.


  •  

    Posting Permissions

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