3

I try to avoid repetition in my code as much as possible.

I know you can turn this:

variable = variable+2

into this

variable += 2

but how can I avoid repetition here? Surely there is a way...

this.className = this.className.replace("classname","")
william malo
  • 2,480
  • 2
  • 18
  • 18
  • Well, you can always roll a function that does the job. Then you don't have to write things twice each time. – pimvdb Mar 10 '12 at 18:33

3 Answers3

3

Unfortunately the answer is no, there is no shorthand for that type of statement.

DJ Quimby
  • 3,669
  • 25
  • 35
1

Make a function to do what you are wanting? As far as a += I'm not aware of a way to overload the operators in javascript... see

Overloading Arithmetic Operators in JavaScript?

Community
  • 1
  • 1
Jared
  • 5,840
  • 5
  • 49
  • 83
0

If you can use JQuery you can call

$(this).removeClass('classname');
Pascal Piché
  • 600
  • 2
  • 13
  • 2
    Or indeed in modern browsers `this.classList.remove('classname')` - either way is more reliable than a string replace! – bobince Mar 10 '12 at 18:46
  • @bobince This is exactly what I was looking for! Thanks man! Too bad I can't mark this as the chosen answer. – william malo Mar 10 '12 at 20:53