Shooting click event with Trigger does not work as expected

Asked

Viewed 1,263 times

0

I am using the Wordpress plugin Metaslider for slides, and in it is possible to add links to the images.

I created a button and I need to simulate the click, as you were clicking on the currently active image in the slider. I’m using the trigger to do the simulation, but I can’t do the trigger work.

<div class="img-slider">
        <?php
            echo do_shortcode("[metaslider id=135]");
        ?>
        <div class="div-button-box-slider">
            <button class="button saiba-mais"> SAIBA MAIS </button>

        </div>
    </div>

Image of the code generated by the plugin: imagem Script:

<script>

jQuery('.saiba-mais').click(function (e) {
    e.preventDefault();
   console.log( "You clicked!" );
    jQuery(".flex-viewport .slides .flex-active-slide a").trigger("click");
});

  • Maybe your click not within $.ready or onload, try using $(document).on("click", ".saiba-mais", function() {. ..}) ... Possible duplicate of: http://answall.com/q/23573/3635

  • It is within $.ready William. The problem is in Rigger, as the function runs to the console.log.

  • I voted to close as "too wide" because you didn’t present the HTML structure, but I withdrew the vote to close. I hope my answer will help.

  • In your code I don’t see those classes flex.viewport nor slides. I’d change the jQuery to jQuery(".flex-active-slide a").click();

2 answers

0


  • It means the dial was wrong?

  • It was the position of the target variable that was missing. As an object is returned, the URL is the first position.

  • The only thing you changed were the dial to ".flex-active-slide a" and extracted the target from a jquery object for gift using [0]. I did not understand the "position of the variable"

  • I expressed myself wrong. It is that the return is an object, and the first element is the link, so I pass the position [0]. I changed the selectors to not get big, but they exist, are generated by the plugin Metaslider.

  • You mean to change target.trigger("click"); why did you make a difference target[0].click();?

0

It is likely that the selector is wrong, to make the test use the console.log (I noticed that you are already familiar with advanced features of browser and javascript so I will not go into detail), example:

jQuery('.saiba-mais').click(function (e) {
    e.preventDefault();
    var target = jQuery(".flex-viewport .slides .flex-active-slide a");
    console.log("You clicked!", target.length);
    target.trigger("click");
});
  • In the console.log should appear something like:

    You clicked! , 2

  • If it turns up:

    You clicked! , 0

    Means the selector is incorrect. The 0 indicates that no elements have been found.

Browser other questions tagged

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