Button inside popup doesn't trigger click event outside the popup

1.4k views Asked by At

I have a custom div like a checkbox to set a condition and also I have a pop up containing a button to trigger the condition.

However, I dont know why, when triggering the click event of custom div from the button of the pop up. it doesn't work.

It works fine if I trigger with a button outside the popup.

Here is the demo of it: https://jsfiddle.net/yusrilmaulidanraji/ayLamku6/8/

The green rectangle's color should be changed when the "Pop Up Trigger" button is clicked.

$(".simon-toggle-accepted, .simon-toggle-declined").on("click", function () {
    var value = $(this).data("value");
        var tr = $(this).closest("tr");
    if (value == "Accepted") {
            if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
                $(this).css("background-color", "green");
                $(this).siblings("div").css("background-color", "white");
            } else {
                $(this).css("background-color", "white");
                $(this).siblings("div").css("background-color", "white");
            }
        }
        else if (value == "Declined") {
            if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
                $(this).css("background-color", "red");
                $(this).siblings("div").css("background-color", "white");
            } else {
                $(this).css("background-color", "white");
                $(this).siblings("div").css("background-color", "white");
            }
        }
});

$("#trigger").on("click",function (e) {
 e.preventDefault();
 $(".simon-toggle-accepted").trigger("click");
  console.log("Test Trigger");
});

$("#dlg-opt-submit").on("click",function (e) {
 e.preventDefault();
  console.log("Test Popup Trigger");
 $(".simon-toggle-accepted").trigger("click");
  console.log($(".simon-toggle-accepted").length);
 $("#popupLogin").popup("close");
});
.simon-toggle {
    width: 90px;
    height: 30px;
}
.simon-toggle-accepted {
    background-color: white;
    border: 1px solid green;
}
.simon-toggle-declined {
    background-color: white;
    border: 1px solid red;
}
.simon-toggle-ignored, .simon-toggle-accepted, .simon-toggle-declined {
    width: 30px;
    height: 30px;
    display: inline-block;
    border-radius: 2px;
    margin-left: 0.5px;
    margin-right: 0.5px;
}
<link href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
  
  
<div class="simon-toggle">
<div class="simon-toggle-accepted" data-value="Accepted"></div>
<div class="simon-toggle-declined" data-value="Declined"></div>
</div>
<button id="trigger">Trigger</button>

<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Pop Up</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
    <form>
        <div style="padding:10px 20px;">
            <button type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b ">Pop Up Trigger</button>
        </div>
    </form>
</div>

2

There are 2 answers

0
kennasoft On BEST ANSWER

You did not add an id attribute to the submit button in the popup...

$(".simon-toggle-accepted, .simon-toggle-declined").on("click", function () {
    var value = $(this).data("value");
        var tr = $(this).closest("tr");
    if (value == "Accepted") {
            if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
                $(this).css("background-color", "green");
                $(this).siblings("div").css("background-color", "white");
            } else {
                $(this).css("background-color", "white");
                $(this).siblings("div").css("background-color", "white");
            }
        }
        else if (value == "Declined") {
            if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
                $(this).css("background-color", "red");
                $(this).siblings("div").css("background-color", "white");
            } else {
                $(this).css("background-color", "white");
                $(this).siblings("div").css("background-color", "white");
            }
        }
});

$("#trigger").on("click",function (e) {
 e.preventDefault();
 $(".simon-toggle-accepted").trigger("click");
  console.log("Test Trigger");
});

$("#dlg-opt-submit").on("click", function (e) {
 e.preventDefault();
  console.log("Test Popup Trigger");
 $(".simon-toggle-accepted").trigger("click");
  console.log($(".simon-toggle-accepted").length);
 $("#popupLogin").popup("close");
});
.simon-toggle {
    width: 90px;
    height: 30px;
}
.simon-toggle-accepted {
    background-color: white;
    border: 1px solid green;
}
.simon-toggle-declined {
    background-color: white;
    border: 1px solid red;
}
.simon-toggle-ignored, .simon-toggle-accepted, .simon-toggle-declined {
    width: 30px;
    height: 30px;
    display: inline-block;
    border-radius: 2px;
    margin-left: 0.5px;
    margin-right: 0.5px;
}
<link href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
  
  
<div class="simon-toggle">
<div class="simon-toggle-accepted" data-value="Accepted"></div>
<div class="simon-toggle-declined" data-value="Declined"></div>
</div>
<button id="trigger">Trigger</button>

<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Pop Up</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
    <form>
        <div style="padding:10px 20px;">
            <button type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b " id="dlg-opt-submit">Pop Up Trigger</button><!-- this was missing id="dlg-opt-submit" -->
        </div>
    </form>
</div>

0
Gerard On

At the end you need to replace this code. You didn't include the ID for the button.

<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
      <form>
        <div style="padding:10px 20px;">
            <button id="dlg-opt-submit" type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b ">Pop Up Trigger</button>
        </div>
      </form>
    </div>