RE: Help me with my new website!
January 9, 2018 at 8:38 pm
(This post was last modified: January 9, 2018 at 8:41 pm by bennyboy.)
(January 9, 2018 at 1:45 pm)FlatAssembler Wrote: I don't understand how your solution could be faster. JQuery, as far as I am aware, searches the entire DOM tree (which is a linear-time algorithm with respect to the number of elements on your page, since the DOM tree isn't a red-black tree or a heap or anything that would allow a logarithmic-time search) and checks if an element matches the selector (such as ".rijecUDrugomDijelu"), and then it sets its "onclick" method to be the function you passed to it.
There's no speed issue either way, though your way will obviously result in more code, especially if you have a lot of rows of data. Unless you're running the site on a toaster, either way will be fine.
But there's value in beauty, and there are programming advantages in separating information, information processing, and visual presentation. When you're trying to do one thing in one part of your page, a hack job is fine. But when you decide to extend your simple code into a huge page with a lot going on, you'll be glad if you keep everything separate (and well documented, of course, even though everything seems clear right now). But let me give you an insight into web programming that might not be obvious. When you want to debug your page, you will be inspecting your page using developer tools-- and in this case, minimal output will make it MUCH easier to notice and eventually fix problem code.
That being said, I have to confess that I have the same thing sometimes, though I arrive at it differently. In ASP.NET (Hi, Kevin!), I build user controls which include javascript. So I might have a user control which includes an image, a couple buttons, and some programming logic, and then populate a page with a hundred of those based on results of a SQL query or something. So it renders out much like yours will: lots and lots of instances of identical code.