0
<div id="a">
    <div id="b">
        <script type="text/javascript">
            var MyDiv1 = GetMyOwnDiv();
        </script>
    </div>
    <div id="c">
        <script type="text/javascript">
            var MyDiv2 = GetMyOwnDiv();
        </script>
    </div>
</div>

MyDiv1 should contain the DOM-Part of div id="a" and MyDiv2 the DOM part of id="b". So that I don't need arguments to get it, like the "this()" or "self()" function / object.

btw. a function to get the child "script" tag dom part of the javascript code inside the script tag.

EDIT: a more detailed example.

//---Global loaded script in HEAD---
function LoadAsyncScript(){
    //.....
    var TScriptData = xmlhttp.responseText;
    var TAppID = G_UNIQUEID;
    var TScript = document.createElement('script');
    TScript.setAttribute('type', 'text/javascript');
    TScript.innerHTML = TScriptData;
    document.getElementById('div_main').insertAdjacentHTML('beforeend', '<div id="_' + TAppID + '_app" style=""></div>');
    var TApp = document.getElementById('_' + TAppID + '_app');
    TApp.dataset['appid'] = TAppID;
    TApp.appendChild(TScript);
    //.....
};

function CloseApp(V_AppID){
    var TMyAppDiv = document.getElementById('_' + V_AppID + '_app');
    TMyAppDiv.removeChild(TMyAppDiv);
};
//---END OF Global loaded script in HEAD---


//Async loaded and "appendChild" script part
function OnClick_Callback(){
    var TMyDiv = GetMyDivOfThisFunction();
    CloseApp(TMyDiv.dataget['appid']);
};
//......

How I can get this work?

Thx so much.

xunwichtig
  • 11
  • 3
  • The question is: *why* do you need this? This smells like an [XY problem](http://xyproblem.info/). Can you tell us what your actual goal is? Because I don't think there's a straightforward way to do this, and it seems completely unnecessary anyway. –  Oct 03 '18 at 21:20
  • 2
    `document.currentScript.parentNode` – rmn Oct 03 '18 at 21:24
  • @rmn ... it sounds good, but, i got "null" back on "document.currentScript" – xunwichtig Oct 03 '18 at 21:59
  • @Chris G ... its quite a bit complicated .. but ... i load scrips async and want to remove it including the holding div of the script – xunwichtig Oct 03 '18 at 22:00
  • @xunwichtig `document.currentScript` will only return the node during the initial processing. It will not work in callbacks. If you need to remove the script tag, why not give it an `id` and hardcode the line? Also, if you're doing this to avoid people looking at your script, note that they still can. –  Oct 03 '18 at 22:14
  • @xunwichtig See, it's an XY problem. Knew it. Anyway, you have `var TScript = document.createElement('script');`, with `TScript` being a perfectly fine reference to the ` –  Oct 03 '18 at 22:19
  • @Chris G ... currentScript: aaah ... ok ;) id: well .. i don't know, how many scripts will be loaded .. well .. u can see this more like a os with applications where dynamicaly loaded incl app-windows, and so one .. and, i don't want to pass a lot of id's around all functions, callback, and requests inside the scripting-structures ... so. the easy and "looks good" way to solve it would be a way to get a parent dom element of the script to remove easly remove the script scope and his build up child html elements, without foreach'ing all dom elements to find all parts of it. – xunwichtig Oct 03 '18 at 23:02
  • So once the page's loaded, you're picking some javascript via ajax and putting them in dynamically created `script` tags and appending them in dynamically created `div` tags which you then append to a `div` tag which was created during the page load, is that right? – rmn Oct 04 '18 at 07:32
  • well ... yes .. on page load, i add some script in the head for the management .. this management can dynamically load script via ajax from the server (on users request (click on a button)) and then it place it in your descripted way into the page ... thats right. – xunwichtig Oct 04 '18 at 12:13

1 Answers1

-2

There are two ways to get Dom objects of Html element.
Assume for the Div with id="a"

  1. Javascript : document.getElementById('a');
  2. Jquery : var contents = $('#a');

Cordialement

LPGTE SOFTS
  • 120
  • 8
  • Where is `id="contents"` in his HTML? – Barmar Oct 03 '18 at 21:36
  • @N.Anouar ... thx, but i can't use JQuery and .. the first way need a "argument" like "contents" ... and, i dosn't have a argument ;) – xunwichtig Oct 03 '18 at 22:17
  • You can tag the Div with CSS Classes for tag, for main div the class "maindiv", for the child div the class "childdiv" and use the closest function to find the previous parent – LPGTE SOFTS Oct 04 '18 at 22:27
  • var $divparent = $('#b').closest('.childdiv'); if ($divparent ).length) { var MyDiv1=$divparent; } else { var MyDiv1=$('#b').closest('.maindiv'); }; var $divparent = $('#c').closest('.childdiv'); if ($divparent ).length) { var MyDiv1=$divparent; } else { var MyDiv1=$('#c').closest('.maindiv'); }; – LPGTE SOFTS Oct 04 '18 at 22:31
  • @N.Anouar ... "closest" includes the potentiality of wrong results. So, thats not a option. And, additionaly .. it needs an argument ;) – xunwichtig Oct 05 '18 at 07:46
  • If closest not fonctional,loop throwght Dom elements and get html using each function. – LPGTE SOFTS Oct 05 '18 at 09:49
  • $('.class').each(function(i){ var domelemnt = $(this).html(); }); – LPGTE SOFTS Oct 05 '18 at 09:51