0

So I have following js code:

jQuery('.first_button').click(function(e) {         
    var get_the_id= jQuery(this).data("custom_id"); 
    var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false;
        if(!isMobile) {
            // if it is not mobile, the first click needs to trigger this function
            jQuery('.second_button_' + get_the_id).custom_plugin();                                     
        }else{
        //something else
        }   
})  

Here, when the first_button is clicked, it gets the custom_id as a variable and looks for second_button_"get_the_id" class. The issue is that the first click does not trigger the main function ("custom_plugin") and a second click is required.

How can I edit it so that the first click triggers the function (so only one click is required)?

Steve Kim
  • 5,293
  • 16
  • 54
  • 99
  • 1
    Is `isMobile` set correctly on the first click? Are you sure the issue lies in this code, and not in the initialisation of `custom_plugin()`? – Rory McCrossan Nov 13 '15 at 09:06
  • mhmm, there is `isMobile` variable just before the code and I omitted `"}else{"`. I will update the question for clarity – Steve Kim Nov 13 '15 at 09:07
  • It should work in a single click if `isMobile = false` and `custom_plugin` does it job when called the first time. – Charlie Nov 13 '15 at 09:08
  • Oddly enough it requires a second click. Here is jsfiddle: https://jsfiddle.net/5e9e2k6y/10/ If you look at the last button (last part of js), it will show that it requires two clicks. – Steve Kim Nov 13 '15 at 09:11
  • Also, the first click saves the attribute (example, `data-custom_id`) and looks for specific button. I got an explanation from the plugin author (http://stackoverflow.com/questions/33682521/custom-attribute-requiring-double-click-featherlight-lightbox/33688513#33688513), but having a bit of hard time figuring it out. – Steve Kim Nov 13 '15 at 09:14
  • jQuery has a magically helpful `fn` called `one(type,callback)`. – roomcayz Nov 13 '15 at 09:16
  • lol, any examples for this magical fn? – Steve Kim Nov 13 '15 at 09:20
  • why are you selecting by `data-my_id` attriubte? – madalinivascu Nov 13 '15 at 10:03
  • you are trying to display diferent content based on user device? – madalinivascu Nov 13 '15 at 10:11
  • Hi. so there are 10 ish posts which each of them have their own `data-post_id`. I want to show the lightbox (the plugin) based on the `post_id`. Because of this, I need to include the post id variable as a part of the selector. – Steve Kim Nov 13 '15 at 10:18
  • In other words, I am trying to trigger the events based on the different post id. Thus, I need to include the first "click" event to save the post id and look for the selector that has that particular post id. Hope I am making sense :) – Steve Kim Nov 13 '15 at 10:19

0 Answers0