Well, it depends how you set things up.
The idea with the ID is certainly doable.
For example, if I set it up so the cost (in a hidden input) is right before my quantity box, then my cost box was after that, I could then get the value of the next to get the value, then multiply that with the quantity and put it in the next box on change of the quantity box.
If that seems too complex, an easy way would be to just give everything an ID. Or, you could set group things together in a div or list, give the container an ID, a class to each of the objects, then get them as appropriate.
Btw, I mentioned it, but just in case it wasn't obvious and you didn't already know, you can load the cost from the database into a hidden input if you don't want it visible for some reason. =p