Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select elements with complex id's

I have elements in my page with ids like child_0_1_1_0.
It is formed with four variables, so there can be maximum of 16 child elements.

What will be the best way to select element:

  1. to form id using string operations from four variables?
  2. Selecting nth child, using nth child property?

Trade off is between touching the DOM Vs string operations.

Which one is better?

like image 305
Mike Avatar asked Jan 30 '26 04:01

Mike


1 Answers

Touching the DOM is almost always the slowest thing you'll do in JavaScript. String operations would be your best bet.

With that said, the difference for most tasks will be insignificant. Don't worry about optimizing your code too early; if n-th child operations allow you to code more easily and cleanly, then use them! If they prove too slow in the future, address it then.

like image 108
Travis Watson Avatar answered Feb 01 '26 16:02

Travis Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!