jquery - how to combine functions for elements with consecutive IDs -


sorry if title confusing, not think of better. basically, want straight forward. code

jquery('.button1').click(function() {     jquery(".box1").slideup();     jquery(this).remove(); }); jquery('.button2').click(function() {     jquery(".box2").slideup();     jquery(this).remove(); }); jquery('.button3').click(function() {     jquery(".box3").slideup();     jquery(this).remove(); }); 

and on.... that's pretty mess when have more boxes. there way can combine them , let jquery id numbers?

you can set class name buttons (e.g. "buttons") , use id:

<button id="button1" class="buttons">delete</button> 

then, can make trick number id:

$(".buttons").on("click", function() {     var n = this.id.replace("button", "");     $(".box" + n).slideup();     $(this).remove(); }); 

demo: http://jsfiddle.net/dxzku/


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -