Results 1 to 4 of 4

Thread: Checking for elements.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Checking for elements.

    Hello all again,
    Can anyone tell me how do I check if any element has already been created(dynamically) and stop it from being generated again if it finds one instance of the object?? For e.g I have this code:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Divs</title>
    <style>
    .rolloverdiv {
    background-color: #F8F8F8;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #333333;
    display: none;
    padding: 4px;
    border: 1px solid silver;
    position: absolute;
    left: 10px;
    top: 10px;
    }
    </style>
    <script>
    function showtip(id, event, tip){
    //var Div = document.getElementById(id);
    var Div = document.createElement('div');
    divId = new Object();
    divId.id = Div;
    divId.id.innerHTML = tip;
    divId.id.style.display = 'block';
    var x=event.clientX;
    var y=event.clientY;
    var Y = y+20;
    divId.id.style.left = x+'px';
    divId.id.style.top = Y+'px';
    }
    
    function hidetip()
    {
    divId.id.style.display = 'none';
    }
    </script>
    </head>
    
    <body>
    <!--<div id="tip" class="rolloverdiv"></div>-->
    <a href="" onMouseMove="showtip('tip', event, 'Hello, this is a text')" onMouseOut="hidetip()">This</a> is a text
    </body>
    </html>
    It uses a div in the HTML initially but I need to change it so that the user(me) doesn't have to write that <div></div> in HTML again and again and it gets generated automatically. But each time the user hovers over the link it creates a new instance of the div how do I stop it from being created each time?? Do I simply create the object as soon as the window loads or what??

    Thanks for reading this post.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Remove the object on mouseOut:
    Code:
    document.body.removeChild(DIV)
    - Mike

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

    Default

    Do I simply create the object as soon as the window loads or what??
    That's the best idea, yes.
    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!

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey!!!

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
  •