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
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts

    Multi document.getElementById string?

    Hey very quick question how could I do this?

    Basicly I have this html code.
    Code:
    <div id="editor">
      <div id="tools"></div>
    </div>
    Now my javascript mods the editor by using the 'editor' id how can I use the variable for the 'editor' to look inside itself for the id 'tools', a bit like this and I know this doesn't work as I tried it but how could I do this.

    Code:
    var editor = document.getElementById('editor'),
        tools  = editor.getElementById('tools');
    // Basicly it will have the editor as the root so
    // if tools is also somewhere else on the page it
    // wont pick it up, id's will never be 2 the same
    // but just in-case there is lol.
    Last edited by DJCMBear; 12-23-2010 at 05:26 AM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question Clarification needed ...

    I'm a bit unclear about what it is that you want to do,
    but for a possible guess, try this (see comments in red)
    Quote Originally Posted by DJCMBear View Post
    Hey very quick question how could I do this?

    Basicly I have this html code.
    Code:
    <div id="editor">
      <div id="tools">Contents of Tools</div>
    </div>
    Now my javascript mods the editor by using the 'editor' id how can I use the variable for the 'editor' to look inside itself for the id 'tools', a bit like this and I know this doesn't work as I tried it but how could I do this.

    Code:
    var editor = document.getElementById('editor'),
        tools  = editor.getElementById('tools');
    // Basicly it will have the editor as the root so
    // if tools is also somewhere else on the page it
    // wont pick it up, id's will never be 2 the same
    // but just in-case there is lol.
       alert(tools.id);
       alert(tools.innerHTML);

  • #3
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    I managed to get around it by writing this var tools = document.getElementById('editor').getElementsByTagName('*'); and then checking the id with a for loop, as I read that you can't do the way I wanted to do.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

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