Vitaliy Vitaliy - 6 months ago 50
jQuery Question

Javascript: automatize function

Good day,

I have a piece of code that is aimed to perform action on mouse click. First I have made a function that scrolls screen on element when I perform a click on another element:

(function($) {
$.fn.goTo = function() {
$('html, body').animate({
scrollTop: $(this).offset().top + 'px'
}, 'fast');


than I assign this function to specific DOM elements:

$('#collapse1').on('', function () {

$('#collapse2').on('', function () {

$('#collapse3').on('', function () {

$('#collapse4').on('', function () {

"" is actually from bootstrap collapse.js.
"This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete)."
The code is working but it is really not looks good. Is there the way to make some sort of cycle? Standard "for" is not working:

var collapseNumber = jQuery.makeArray(document.getElementsByClassName("panel panel-default"));

for (var i = 0; i < collapseNumber.length; i++) {

$('#collapse' + i).on('', function () {
$('#s' + i).goTo();

created array is for getting actual number of elements, which I need to put in the cycle.


The problem you have is the infamous for loop issue where the value of i is the last value. But there is no reason to have to loop when a simple data attribute can be used.

Just use a data-attribute to select and link the things

<div data-goto="#s1">...</div>

and the JavaScript

$('[data-goto]').on('', function () {
    var selector = $(this).data("goto");