javascript - How can I reduce the copy-paste code? Refactor? -


how can refactor piece of code contains less copy-paste code?

$("#hnv_4").click(function(e){     manipulate(4);     e.stoppropagation(); }); $("#hnv_3").click(function(e){     manipulate(3);     e.stoppropagation(); }); $("#hnv_2").click(function(e){     manipulate(2);     e.stoppropagation(); }); $("#hnv_1").click(function(e){     manipulate(1);     e.stoppropagation(); }); 

can use loop here minimize code or maybe jquery?

i tried:

for (i = 1; <= 4; i++) {  $("#hnv_" + i).click(function (e) {     alert(i);  }); } 

but @ end .. alert shows 5 always

yes,

$("[id^='hnv_']").click(function(e) {     var number = number(this.id.split("_")[1]);     manipulate(number);     e.stoppropagation(); }); 

Comments