If the proceedings in a general election contest under this division are dismissed for insufficiency or for want of prosecution, or the election is confirmed by the court, judgment for costs shall be rendered against the contestant and in favor of the defendant. If the election is annulled or set aside on the ground of errors of a precinct board in conducting the election or in canvassing the returns, the costs shall be a charge against the county or city where the election was held. When the election is annulled or set aside on any other ground, judgment for costs shall be given in favor of contestant and against the defendant.