Most seem to be improvised on the spot or prepared for with an expected appearance. There's not too much direct usage against Romans, mainly Epirotes, Carthaginians and Persians (in the order encountered).
With the war with the Epirotes, under Pyrrhus, the first engagement featuring elephants at Heraclea took the Romans by surprise and it was an Epirote victory, the following year at Asculum the Romans came prepared for elephants with spiked chariots, flammable projectiles and other "anti-elephant" weaponry, leading to the infamous "Pyrrhic victory" for the Epirotes, where Pyrrhus won the day, but at a huge cost to his own (mostly Macedonian) forces.
Some accounts of Roman actions, other than those previously mentioned, say that loud noises such as horns frightening elephants, peppering them with javelins (pila), thrusting spears (hastae) in their unarmoured sides and also cutting off the trunk were effective countermeasures.
A funnier example and one which has also gained fame through games like Rome: Total War, is the usage of flaming pigs, which derives from Aelian's On Animals where he cites an example of Megara (a Greek city in western Attica) besieged by Macedonian forces under Antigonus II, where to combat the Macedonian war elephants, the Megarians smeared pigs with pitch, set them alight and goaded them towards the enemy's elephants, with the flames and squeals of the pigs "driving them mad and throwing them into terrible confusion". This action by the Megarians ended up breaking the siege of their city by the Macedonians, according to the account.
There's some later examples of war elephants in the medieval ages, mostly in South and South-East Asia as well, but that's not ancient, unfortunately.
I think it is important to note that war elephants were very dangerous to even their own side. Dodge in his work on the second punic war stated that elephants were an uncertain feature and that if they acted successfully it was in conflicts with forces which did not know how to deal with them. They could easily turn on their own side and for this reason drives carried spikes and mallets to kill the elephants during the second punic war. According to Dodge the Romans used flaming arrows in order to scare Pyrrhus' elephants at the battle of Beneventum, the elephants turned and caused Pyrrhus' defeat.
"Hannibal, Theodore Ayrault Dodge"