1

HTML document has some HTML-Elements such as div, p, img, ...

Most of them are dynamically created ( if helpful css: "position:absolute;").

Every time after OnClick the element has to come to the front of other elements.

I mean:

element.parentNode.appendChild(element);
or
element.parent().append(element);

or

// E.g. with css as follows:
$('#ObjId').css('z-index', HighestIndx+1 );

I prefer the use of appendChild, due to the css style z-index won't be inserted into the element. But I don't know whether the choice of appendChild vs z-index would be better.

My question: What is better to use z-index or append/appendChild ? Thanks.

charlietfl
  • 170,828
  • 13
  • 121
  • 150
user3815508
  • 369
  • 4
  • 20

2 Answers2

2

I would use z-index because I assume its faster and reliable in old browsers but anyway that's me.

Here is a useful article: Why would appendChild disregard zIndex?

The article implies some points but isn't entirely focused to your question.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Mudassir
  • 1,136
  • 1
  • 11
  • 29
1

I don't think that appendChild() has anything to do with the z-index. It just add a new child to the calling parent to bottom as a last child. If you want to show your div in front of the other div then it is recommended that you should go with the z-index property.

Jayant Varshney
  • 1,765
  • 1
  • 25
  • 42
  • That's right. But it can be also used in order to bring a object to front of others. But now I think too that the use of z-index will be better [Another link](http://stackoverflow.com/questions/3233219/is-there-a-way-in-jquery-to-bring-a-div-to-front). – user3815508 Oct 18 '14 at 21:53