Automating a function each JS/Jquery

Asked

Viewed 310 times

1

I have a problem automating a function in JS / Jquery. I have the following code:

        $('.tab-pane').each(function(index, el) {
          tipo = $(this).attr('id');
          $("#owl-carousel-" + tipo).owlCarousel({
            loop: true,
            nav: true,
            items: 1,
            dots: true
          });
          $('#owl-carousel-' + tipo + ' .owl-page').each(function(index, el) {
            numberTotal = index;
          });
          $('#numbertotal' + tipo).text(numberTotal + 1);


          // Custom Navigation Events
          $("#" + tipo + " .btn-next").click(function() {
            $("#owl-carousel-" + tipo).trigger('owl.next');
            verificaActive(tipo);
          });
          $("#" + tipo + " .btn-prev").click(function() {
            $("#owl-carousel-" + tipo).trigger('owl.prev');
            verificaActive(tipo);
          });
        });

I iterate on all Ivs that have the class ". tab-pane" and take your ID as a parameter for the rest of the function. So far everything ok, the problem is in the click function, because it is assigning only the last iteration and about write them.

Does anyone know a way to do this without taking parameters from the click button to be automated?

Thanks in advance ;)

EDIT

@juniorNunes here is the HTML, you will have other blocks like this changing the ID. The button block has to be one for each slide and accurate through that script add functions to change between slides.

I added the "data-type" in the buttons just to work, but this way I have to add in all buttons and to avoid errors if you forget or any typo the ideal would be to catch this type by Js only by the parent div ID.

<div role="tabpanel" class="tab-pane" id="central">
  <div class="owl-carousel" id="owl-carousel-central">
    <div class="item">
      <span>Slide 1</span>
    </div>
    <div class="item">
      <span>Slide 2</span>
    </div>
    <div class="item">
      <span>Slide 3</span>
    </div>
  </div>
  <div class="paginacao">
    <div class="btn-owl btn-first" data-tipo="central">
      <<</div>
        <div class="btn-owl btn-prev" data-tipo="central">
          <</div>

            <div class="contagem-slides">
              <span id="numbercurrentcentral"></span><span> de <span id="numbertotalcentral">8</span></span>
            </div>

            <div class="btn-owl btn-next" data-tipo="central">></div>
            <div class="btn-owl btn-last" data-tipo="central">>></div>
        </div>
    </div>
  • 2

    This is SO English, Please Translate your Question to Portuguese or post it here http://stackoverflow.com/questions/ask . Welcome, http://answall.com/tour

  • You already try to make one console.log(tipo) just to see if it’s right, because if it’s changing value I’m not seeing because it was about writing.

  • Opaaa, can you post the HTML code and say what exactly do you want to occur? pro personal help you better.

  • @Joserodrigues the type is running, so much so that the slide is running which is generated on the line "$("#Owl-Carousel-"+type). owlCarousel({...".

  • @Juniornunes has a clear face.

1 answer

1


See if this will work:

$('.tab-pane').each(function(index, el) {
  var id = $(el).prop('id');

  $(el).owlCarousel({
    loop:true,
    nav:true,
    items:1,
    dots: true
  });

  $(el).find('.owl-page').each(function(index, elm) {
    numberTotal = index;    
  });

  $('#numbertotal'+id).text(numberTotal+1);

  // Custom Navigation Events
  $(el).find('.btn-next').click(function(){
    $("#owl-carousel-"+id).trigger('owl.next');
    verificaActive(id);
  });
  $(el).find('.btn-prev').click(function(){
    $("#owl-carousel-"+id).trigger('owl.prev');
    verificaActive(id);
  });
});
  • It worked my dear, thank you very much. The problem was in the "$(el). find('. btn-next')", you know tell me the difference of this call to the one I was making directly in the element "$("#" + type + " . btn-Prev")"?

  • @Wellingtonfjr I would say that the difference is actually in its variable tipo, see that in his she was stated somewhere else then her scope was greater than the function, and that in the Juniornunes he changed her statement, which in case he called her id, as it is declared within the function the scope of it boils down to just that function

  • @Wellingtonfjr what was wrong was your reference to reach the buttons: .btn-next and .btn-prev if you notice before placing these buttons you put an id that does not exist, it would be the value of the type variable (central). and according to your html it is non-existent.

  • But this id exists, does it not recognize the "type" variable I created at the beginning of the script? I used it to create the selector, so this "$("#" + type + " .btn-Prev")" would look like this "$("#central .btn-Prev")", and the . btn-Prev is inside the div with central id. .

  • True, now that I’ve noticed. It may be that your other Divs have the same id, and he may be cluttering it up while running...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.