This code feels very repetitive, and I am sure there is a more efficient way to write it. Any help would be much appreciated.
jQuery:
$(function(){
$('.perry_1_txt').keyup(function(){
$('.perry_1').text($(this).val());
});
$('.perry_2_txt').keyup(function(){
$('.perry_2').text($(this).val());
});
$('.perry_3_txt').keyup(function(){
$('.perry_3').text($(this).val());
});
$('.perry_4_txt').keyup(function(){
$('.perry_4').text($(this).val());
});
$('.perry_5_txt').keyup(function(){
$('.perry_5').text($(this).val());
});
$('.perry_6_txt').keyup(function(){
$('.perry_6').text($(this).val());
});
});
HTML:
<div>
<input class="perry_1_txt" /><span class="perry_1"></span>
<input class="perry_2_txt" /><span class="perry_2"></span>
<input class="perry_3_txt" /><span class="perry_3"></span>
<input class="perry_4_txt" /><span class="perry_4"></span>
<input class="perry_5_txt" /><span class="perry_5"></span>
<input class="perry_6_txt" /><span class="perry_6"></span>
<input class="perry_7_txt" /><span class="perry_7"></span>
</div>
The code dynamically takes the value from the inputs and places them in the corresponding span. It works fine, but it less than elegant.
Use fewer class names, and store the index somewhere else.
HTML
JavaScript
Edit
Okay, so you’ve shown some HTML. We can do even better, and don’t need a single class name:
HTML
JavaScript