Assigning a value to a variable on a click event in jQuery











up vote
1
down vote

favorite












I'm pretty new to JavaScript and jQuery, and I could really use some help here.



I have a click event and a keyup event, where the keyup event should only be available if the click event has happened.



I have a series of images, each of which, when clicked, should trigger a modal image.



<!-- Images -->
<div class="col1">
<!-- Trigger the Modal -->
<img class="storyboardImg" id="1" src="storyboard-1.jpg"></img>
</div>
<div class="col1">
<img class="storyboardImg" id="2" src="storyboard-1.jpg"></img>
</div>
<div class="col1">
<img class="storyboardImg" id="3" src="storyboard-1.jpg"></img>
</div>


<!-- The Modals -->
<div id="storyboardModal" class="modal">
<!-- The Close Function -->
<span class="closeModal" onclick=
"document.getElementById('storyboardModal').style.display='none'">
x&times;</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>


When the modal image is activated by the click event in JS below, only then it should allow navigating to the previous and next images in the series using the left and right arrow keys on the keyboard (which should be made available with the keyup function).



I have tried introducing a variable called "modalActive", which is set to "0" by default, and as part of the click event, this variable should be assigned a new value of "1". The idea is that the keyup event should only be available when the value of "modalActive" is "1", as shown below. However, it doesn't seem to read the keyup function, when the variable value is "1".



<script>
var modal = document.getElementById('storyboardModal');

var img = $('.storyboardImg');
var modalImg = $("#img01");
var captionText = document.getElementById("caption");

var currentId = null;
var modalActive = "0";

$('.storyboardImg').click(function(){
var id = this.getAttribute("id");
currentId = id;
modal.style.display = "block";
captionText.innerHTML = this.alt;
var newSrc = this.src;
modalImg.attr('src', newSrc);
modalActive = "1";
})

if (modalActive == "1") {
$(document).on("keyup", function (e) {
// Left Arrow Key
if (e.which===37 && currentId <= 1) {
document.getElementById('storyboardModal').style.display='none';
modalActive = null;
}
else if (e.which === 37 && currentId > 1) {
currentId--;
$(".modal").fadeOut(300);
//$("#storyboardModal" + currentId).fadeIn(300);
modal.style.display = "block";
var newSrc1 = document.getElementById(currentId).src;
modalImg.attr('src', newSrc1);
captionText.innerHTML = this.alt;
}
// Right Arrow Key
else if (e.which === 39 && currentId >= 6) {

document.getElementById('storyboardModal').style.display='none';
modalActive = null;
}
else if (e.which === 39 && currentId < 6) {
currentId++;
$(".modal").fadeOut(300);
modal.style.display = "block";
var newSrc2 = document.getElementById(currentId).src;
modalImg.attr('src', newSrc2);
captionText.innerHTML = this.alt;

}
});
}

/***** close Modal *****/

var span = document.getElementsByClassName("closeModal")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modalActive = null;
$(".modal").fadeOut(300);
}
</script>


It seems like the variable "modalActive" is not getting assigned the new value in the click event. Could someone please point out what I'm doing wrong here?.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm pretty new to JavaScript and jQuery, and I could really use some help here.



    I have a click event and a keyup event, where the keyup event should only be available if the click event has happened.



    I have a series of images, each of which, when clicked, should trigger a modal image.



    <!-- Images -->
    <div class="col1">
    <!-- Trigger the Modal -->
    <img class="storyboardImg" id="1" src="storyboard-1.jpg"></img>
    </div>
    <div class="col1">
    <img class="storyboardImg" id="2" src="storyboard-1.jpg"></img>
    </div>
    <div class="col1">
    <img class="storyboardImg" id="3" src="storyboard-1.jpg"></img>
    </div>


    <!-- The Modals -->
    <div id="storyboardModal" class="modal">
    <!-- The Close Function -->
    <span class="closeModal" onclick=
    "document.getElementById('storyboardModal').style.display='none'">
    x&times;</span>
    <!-- Modal Content (The Image) -->
    <img class="modal-content" id="img01">
    <!-- Modal Caption (Image Text) -->
    <div id="caption"></div>
    </div>


    When the modal image is activated by the click event in JS below, only then it should allow navigating to the previous and next images in the series using the left and right arrow keys on the keyboard (which should be made available with the keyup function).



    I have tried introducing a variable called "modalActive", which is set to "0" by default, and as part of the click event, this variable should be assigned a new value of "1". The idea is that the keyup event should only be available when the value of "modalActive" is "1", as shown below. However, it doesn't seem to read the keyup function, when the variable value is "1".



    <script>
    var modal = document.getElementById('storyboardModal');

    var img = $('.storyboardImg');
    var modalImg = $("#img01");
    var captionText = document.getElementById("caption");

    var currentId = null;
    var modalActive = "0";

    $('.storyboardImg').click(function(){
    var id = this.getAttribute("id");
    currentId = id;
    modal.style.display = "block";
    captionText.innerHTML = this.alt;
    var newSrc = this.src;
    modalImg.attr('src', newSrc);
    modalActive = "1";
    })

    if (modalActive == "1") {
    $(document).on("keyup", function (e) {
    // Left Arrow Key
    if (e.which===37 && currentId <= 1) {
    document.getElementById('storyboardModal').style.display='none';
    modalActive = null;
    }
    else if (e.which === 37 && currentId > 1) {
    currentId--;
    $(".modal").fadeOut(300);
    //$("#storyboardModal" + currentId).fadeIn(300);
    modal.style.display = "block";
    var newSrc1 = document.getElementById(currentId).src;
    modalImg.attr('src', newSrc1);
    captionText.innerHTML = this.alt;
    }
    // Right Arrow Key
    else if (e.which === 39 && currentId >= 6) {

    document.getElementById('storyboardModal').style.display='none';
    modalActive = null;
    }
    else if (e.which === 39 && currentId < 6) {
    currentId++;
    $(".modal").fadeOut(300);
    modal.style.display = "block";
    var newSrc2 = document.getElementById(currentId).src;
    modalImg.attr('src', newSrc2);
    captionText.innerHTML = this.alt;

    }
    });
    }

    /***** close Modal *****/

    var span = document.getElementsByClassName("closeModal")[0];

    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
    modalActive = null;
    $(".modal").fadeOut(300);
    }
    </script>


    It seems like the variable "modalActive" is not getting assigned the new value in the click event. Could someone please point out what I'm doing wrong here?.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm pretty new to JavaScript and jQuery, and I could really use some help here.



      I have a click event and a keyup event, where the keyup event should only be available if the click event has happened.



      I have a series of images, each of which, when clicked, should trigger a modal image.



      <!-- Images -->
      <div class="col1">
      <!-- Trigger the Modal -->
      <img class="storyboardImg" id="1" src="storyboard-1.jpg"></img>
      </div>
      <div class="col1">
      <img class="storyboardImg" id="2" src="storyboard-1.jpg"></img>
      </div>
      <div class="col1">
      <img class="storyboardImg" id="3" src="storyboard-1.jpg"></img>
      </div>


      <!-- The Modals -->
      <div id="storyboardModal" class="modal">
      <!-- The Close Function -->
      <span class="closeModal" onclick=
      "document.getElementById('storyboardModal').style.display='none'">
      x&times;</span>
      <!-- Modal Content (The Image) -->
      <img class="modal-content" id="img01">
      <!-- Modal Caption (Image Text) -->
      <div id="caption"></div>
      </div>


      When the modal image is activated by the click event in JS below, only then it should allow navigating to the previous and next images in the series using the left and right arrow keys on the keyboard (which should be made available with the keyup function).



      I have tried introducing a variable called "modalActive", which is set to "0" by default, and as part of the click event, this variable should be assigned a new value of "1". The idea is that the keyup event should only be available when the value of "modalActive" is "1", as shown below. However, it doesn't seem to read the keyup function, when the variable value is "1".



      <script>
      var modal = document.getElementById('storyboardModal');

      var img = $('.storyboardImg');
      var modalImg = $("#img01");
      var captionText = document.getElementById("caption");

      var currentId = null;
      var modalActive = "0";

      $('.storyboardImg').click(function(){
      var id = this.getAttribute("id");
      currentId = id;
      modal.style.display = "block";
      captionText.innerHTML = this.alt;
      var newSrc = this.src;
      modalImg.attr('src', newSrc);
      modalActive = "1";
      })

      if (modalActive == "1") {
      $(document).on("keyup", function (e) {
      // Left Arrow Key
      if (e.which===37 && currentId <= 1) {
      document.getElementById('storyboardModal').style.display='none';
      modalActive = null;
      }
      else if (e.which === 37 && currentId > 1) {
      currentId--;
      $(".modal").fadeOut(300);
      //$("#storyboardModal" + currentId).fadeIn(300);
      modal.style.display = "block";
      var newSrc1 = document.getElementById(currentId).src;
      modalImg.attr('src', newSrc1);
      captionText.innerHTML = this.alt;
      }
      // Right Arrow Key
      else if (e.which === 39 && currentId >= 6) {

      document.getElementById('storyboardModal').style.display='none';
      modalActive = null;
      }
      else if (e.which === 39 && currentId < 6) {
      currentId++;
      $(".modal").fadeOut(300);
      modal.style.display = "block";
      var newSrc2 = document.getElementById(currentId).src;
      modalImg.attr('src', newSrc2);
      captionText.innerHTML = this.alt;

      }
      });
      }

      /***** close Modal *****/

      var span = document.getElementsByClassName("closeModal")[0];

      // When the user clicks on <span> (x), close the modal
      span.onclick = function() {
      modalActive = null;
      $(".modal").fadeOut(300);
      }
      </script>


      It seems like the variable "modalActive" is not getting assigned the new value in the click event. Could someone please point out what I'm doing wrong here?.










      share|improve this question















      I'm pretty new to JavaScript and jQuery, and I could really use some help here.



      I have a click event and a keyup event, where the keyup event should only be available if the click event has happened.



      I have a series of images, each of which, when clicked, should trigger a modal image.



      <!-- Images -->
      <div class="col1">
      <!-- Trigger the Modal -->
      <img class="storyboardImg" id="1" src="storyboard-1.jpg"></img>
      </div>
      <div class="col1">
      <img class="storyboardImg" id="2" src="storyboard-1.jpg"></img>
      </div>
      <div class="col1">
      <img class="storyboardImg" id="3" src="storyboard-1.jpg"></img>
      </div>


      <!-- The Modals -->
      <div id="storyboardModal" class="modal">
      <!-- The Close Function -->
      <span class="closeModal" onclick=
      "document.getElementById('storyboardModal').style.display='none'">
      x&times;</span>
      <!-- Modal Content (The Image) -->
      <img class="modal-content" id="img01">
      <!-- Modal Caption (Image Text) -->
      <div id="caption"></div>
      </div>


      When the modal image is activated by the click event in JS below, only then it should allow navigating to the previous and next images in the series using the left and right arrow keys on the keyboard (which should be made available with the keyup function).



      I have tried introducing a variable called "modalActive", which is set to "0" by default, and as part of the click event, this variable should be assigned a new value of "1". The idea is that the keyup event should only be available when the value of "modalActive" is "1", as shown below. However, it doesn't seem to read the keyup function, when the variable value is "1".



      <script>
      var modal = document.getElementById('storyboardModal');

      var img = $('.storyboardImg');
      var modalImg = $("#img01");
      var captionText = document.getElementById("caption");

      var currentId = null;
      var modalActive = "0";

      $('.storyboardImg').click(function(){
      var id = this.getAttribute("id");
      currentId = id;
      modal.style.display = "block";
      captionText.innerHTML = this.alt;
      var newSrc = this.src;
      modalImg.attr('src', newSrc);
      modalActive = "1";
      })

      if (modalActive == "1") {
      $(document).on("keyup", function (e) {
      // Left Arrow Key
      if (e.which===37 && currentId <= 1) {
      document.getElementById('storyboardModal').style.display='none';
      modalActive = null;
      }
      else if (e.which === 37 && currentId > 1) {
      currentId--;
      $(".modal").fadeOut(300);
      //$("#storyboardModal" + currentId).fadeIn(300);
      modal.style.display = "block";
      var newSrc1 = document.getElementById(currentId).src;
      modalImg.attr('src', newSrc1);
      captionText.innerHTML = this.alt;
      }
      // Right Arrow Key
      else if (e.which === 39 && currentId >= 6) {

      document.getElementById('storyboardModal').style.display='none';
      modalActive = null;
      }
      else if (e.which === 39 && currentId < 6) {
      currentId++;
      $(".modal").fadeOut(300);
      modal.style.display = "block";
      var newSrc2 = document.getElementById(currentId).src;
      modalImg.attr('src', newSrc2);
      captionText.innerHTML = this.alt;

      }
      });
      }

      /***** close Modal *****/

      var span = document.getElementsByClassName("closeModal")[0];

      // When the user clicks on <span> (x), close the modal
      span.onclick = function() {
      modalActive = null;
      $(".modal").fadeOut(300);
      }
      </script>


      It seems like the variable "modalActive" is not getting assigned the new value in the click event. Could someone please point out what I'm doing wrong here?.







      javascript jquery html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 14:06









      Md. Mokammal Hossen Farnan

      577320




      577320










      asked Nov 11 at 10:56









      Clare P.

      61




      61
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          To get your code working, you should move the modalActive check to inside your keyup function declaration



          $(document).on("keyup", function (e) {
          if (modalActive == "1") {
          // Left Arrow Key
          if (e.which===37 && currentId <= 1) {
          ...


          The reason your code fails is that the code inside the <script> tag is executed only once (on page load) and by that time, the modalActive is false and your keyup event will not be registered and never executed.
          With this change, the event will be registered on page load and the check wether to act on it is instead inside the event function.






          share|improve this answer



















          • 1




            That solved it! Thank you so much, that's a really helpful information!
            – Clare P.
            Nov 14 at 9:45











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248023%2fassigning-a-value-to-a-variable-on-a-click-event-in-jquery%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          To get your code working, you should move the modalActive check to inside your keyup function declaration



          $(document).on("keyup", function (e) {
          if (modalActive == "1") {
          // Left Arrow Key
          if (e.which===37 && currentId <= 1) {
          ...


          The reason your code fails is that the code inside the <script> tag is executed only once (on page load) and by that time, the modalActive is false and your keyup event will not be registered and never executed.
          With this change, the event will be registered on page load and the check wether to act on it is instead inside the event function.






          share|improve this answer



















          • 1




            That solved it! Thank you so much, that's a really helpful information!
            – Clare P.
            Nov 14 at 9:45















          up vote
          0
          down vote













          To get your code working, you should move the modalActive check to inside your keyup function declaration



          $(document).on("keyup", function (e) {
          if (modalActive == "1") {
          // Left Arrow Key
          if (e.which===37 && currentId <= 1) {
          ...


          The reason your code fails is that the code inside the <script> tag is executed only once (on page load) and by that time, the modalActive is false and your keyup event will not be registered and never executed.
          With this change, the event will be registered on page load and the check wether to act on it is instead inside the event function.






          share|improve this answer



















          • 1




            That solved it! Thank you so much, that's a really helpful information!
            – Clare P.
            Nov 14 at 9:45













          up vote
          0
          down vote










          up vote
          0
          down vote









          To get your code working, you should move the modalActive check to inside your keyup function declaration



          $(document).on("keyup", function (e) {
          if (modalActive == "1") {
          // Left Arrow Key
          if (e.which===37 && currentId <= 1) {
          ...


          The reason your code fails is that the code inside the <script> tag is executed only once (on page load) and by that time, the modalActive is false and your keyup event will not be registered and never executed.
          With this change, the event will be registered on page load and the check wether to act on it is instead inside the event function.






          share|improve this answer














          To get your code working, you should move the modalActive check to inside your keyup function declaration



          $(document).on("keyup", function (e) {
          if (modalActive == "1") {
          // Left Arrow Key
          if (e.which===37 && currentId <= 1) {
          ...


          The reason your code fails is that the code inside the <script> tag is executed only once (on page load) and by that time, the modalActive is false and your keyup event will not be registered and never executed.
          With this change, the event will be registered on page load and the check wether to act on it is instead inside the event function.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 at 12:16

























          answered Nov 11 at 11:13









          jonasdev

          664




          664








          • 1




            That solved it! Thank you so much, that's a really helpful information!
            – Clare P.
            Nov 14 at 9:45














          • 1




            That solved it! Thank you so much, that's a really helpful information!
            – Clare P.
            Nov 14 at 9:45








          1




          1




          That solved it! Thank you so much, that's a really helpful information!
          – Clare P.
          Nov 14 at 9:45




          That solved it! Thank you so much, that's a really helpful information!
          – Clare P.
          Nov 14 at 9:45


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248023%2fassigning-a-value-to-a-variable-on-a-click-event-in-jquery%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python