0

So I asked a similar question before... I wanted to know how to argue for the css property in a document.element.style.property=value. The solution was simple, and almost made sense --but clearly I didn't understand it entirely or I'd know trying the same solution for .element doesn't work.

Here is my code:

function appendElement(handle){

 element=document[handle]('div');
 document.body.appendChild(element);
}

This way I could choose to create a new element or shift an existing one based on id or class or index appearance or whatever. Of course even without knowing the correct way to do this, the code I have above looks wrong to me, but it's the best I can do without some assistance.

EDIT: Test case

/* The core instructions */
element=document.createElement('div');
document.body.appendChild(element);


/* the choosy version */
function appendElement(handle){
element=document[handle]('div');
document.body.appendChild(element);
}
appendElement(createElement);
element.innerHTML="third text";


/* SHOULD move the 'text' div under the 'third text' div*/
appendElement(getElementById('first'));
<div id="first">text</div>
<div>second text</div>
Community
  • 1
  • 1
Musixauce3000
  • 549
  • 1
  • 3
  • 11
  • 1
    You want to choose an existing element or create a new one if it doesn't exist based on a selector passed to the function? – Omri Aharon Feb 03 '16 at 18:45
  • 1
    I don't understand what you expect *element* to be here. If it's indeed a property of `document`, then your code will work. If it's not a property of document, then your notional code `document`*.element*`.style` seems wrong (i.e., indicates something different from what you really want). – apsillers Feb 03 '16 at 18:45
  • 1
    if handle is a function name and its a method of document, you are doing it well... but i don't know what is handle, if it is a function, can you put it with the explanation code? – gabrielperales Feb 03 '16 at 18:51
  • So like you could write, document.getElementById() or document.createElement(), for example. At least that's the idea. – Musixauce3000 Feb 03 '16 at 18:52
  • @Masixauce3000 then you should pass the string `"getElementById"` to the function – gabrielperales Feb 03 '16 at 18:53
  • @gabrielperales yes when I return to my desk – Musixauce3000 Feb 03 '16 at 18:53
  • 1
    @Musixauce3000 — It still isn't very clear. Please try creating a *complete* test case — http://stackoverflow.com/help/mcve — and tell us what the expected input and output of the function call should be. – Quentin Feb 03 '16 at 18:53
  • Did you try the code of [my answer](http://stackoverflow.com/a/35185458/1463630)? Let me know if you already have solved your question. – gabrielperales Feb 07 '16 at 19:44
  • @gabrielperales I accepted and up-voted your answer. – Musixauce3000 Feb 08 '16 at 21:00

2 Answers2

-1

Edit

In this line appendElement(getElementById('first'));you are not passing a function as you want, you are passing the result of call undefined with the param 'first', because it can't find a function in that context or a global function called getElementById (so it will be undefined), furthermore your are trying to execute undefined passing it a string... this is going to raise an error, and in the case in which function existed (i.e you pass document.getElementById('first')), then you will be passing the returned value of executing that function instead of the function.

If you want to pass a function you should pass a function, thats is appendElement(document.getElementById), without calling it with an argument, but I think you are going to need to pass a selector to that function to accomplish what you are trying to do. So the code will be something like this:

function appendElement(handle, selector){
  element=handle.call(document, selector);
  document.body.appendChild(element);
}

appendElement(document.getElementById, 'first');
<div id="first">text</div>
<div>second text</div>

call allows you to execute a function as a method and specify which object will be the receptor of that calling. Here is more info

gabrielperales
  • 6,935
  • 1
  • 20
  • 19
  • I was trying to avoid having to use `document.` in the argument, but I think if I ponder on this answer long enough it will come to me why that isn't possible. – Musixauce3000 Feb 08 '16 at 20:59
-3

I didn't understand your need.

But, if handle is equals to 'createElement', which is a property of document, your code will run.