Local variable (in a function) to global (Jquery)










-1















I have four <button> tags, I need that when I click on the button tag, jQuery add the class Active.



This is the code I have:



$(document).ready(function () 
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");


);
);


Sorry for my English



PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)










share|improve this question



















  • 2





    I get this error in the console That's the origin of the error, but not the actual error

    – CertainPerformance
    Nov 15 '18 at 2:44











  • When you create the second click event btnActive is undefined making the selector invalid.

    – Spencer Wieczorek
    Nov 15 '18 at 2:46











  • This is the error line $(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {

    – Lolete Sape
    Nov 15 '18 at 2:49















-1















I have four <button> tags, I need that when I click on the button tag, jQuery add the class Active.



This is the code I have:



$(document).ready(function () 
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");


);
);


Sorry for my English



PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)










share|improve this question



















  • 2





    I get this error in the console That's the origin of the error, but not the actual error

    – CertainPerformance
    Nov 15 '18 at 2:44











  • When you create the second click event btnActive is undefined making the selector invalid.

    – Spencer Wieczorek
    Nov 15 '18 at 2:46











  • This is the error line $(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {

    – Lolete Sape
    Nov 15 '18 at 2:49













-1












-1








-1








I have four <button> tags, I need that when I click on the button tag, jQuery add the class Active.



This is the code I have:



$(document).ready(function () 
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");


);
);


Sorry for my English



PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)










share|improve this question
















I have four <button> tags, I need that when I click on the button tag, jQuery add the class Active.



This is the code I have:



$(document).ready(function () 
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");


);
);


Sorry for my English



PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 2:51







Lolete Sape

















asked Nov 15 '18 at 2:43









Lolete SapeLolete Sape

32




32







  • 2





    I get this error in the console That's the origin of the error, but not the actual error

    – CertainPerformance
    Nov 15 '18 at 2:44











  • When you create the second click event btnActive is undefined making the selector invalid.

    – Spencer Wieczorek
    Nov 15 '18 at 2:46











  • This is the error line $(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {

    – Lolete Sape
    Nov 15 '18 at 2:49












  • 2





    I get this error in the console That's the origin of the error, but not the actual error

    – CertainPerformance
    Nov 15 '18 at 2:44











  • When you create the second click event btnActive is undefined making the selector invalid.

    – Spencer Wieczorek
    Nov 15 '18 at 2:46











  • This is the error line $(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {

    – Lolete Sape
    Nov 15 '18 at 2:49







2




2





I get this error in the console That's the origin of the error, but not the actual error

– CertainPerformance
Nov 15 '18 at 2:44





I get this error in the console That's the origin of the error, but not the actual error

– CertainPerformance
Nov 15 '18 at 2:44













When you create the second click event btnActive is undefined making the selector invalid.

– Spencer Wieczorek
Nov 15 '18 at 2:46





When you create the second click event btnActive is undefined making the selector invalid.

– Spencer Wieczorek
Nov 15 '18 at 2:46













This is the error line $(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {

– Lolete Sape
Nov 15 '18 at 2:49





This is the error line $(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {

– Lolete Sape
Nov 15 '18 at 2:49












3 Answers
3






active

oldest

votes


















0














There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:



$(".cont-btn").click(function () 
$(".cont-btn").removeClass("active");
$(this).addClass("active");
);


Example






share|improve this answer























  • Thank you very very very much, im a retard :facepalm:

    – Lolete Sape
    Nov 15 '18 at 3:04


















0














You are making it more complicated than it needs to be.



The following should do what you need assuming that <button> is inside class="cont-btn"



var $buttons = $(".cont-btn button").click(function () 
$buttons.removeClass('active');
$(this).addClass('active');
);





share|improve this answer






























    0














    $(".cont-btn button:nth-of-type(" + btnActive + ")") this code is executed only once.



    Your code may be like this



    $('.cont-btn button:eq(0)').addClass('active');
    $('.cont-btn button').click(function()
    $('.cont-btn button').removeClass('active');
    $(this).addClass('active');
    )


    P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.






    share|improve this answer






















      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',
      autoActivateHeartbeat: false,
      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%2f53311678%2flocal-variable-in-a-function-to-global-jquery%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:



      $(".cont-btn").click(function () 
      $(".cont-btn").removeClass("active");
      $(this).addClass("active");
      );


      Example






      share|improve this answer























      • Thank you very very very much, im a retard :facepalm:

        – Lolete Sape
        Nov 15 '18 at 3:04















      0














      There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:



      $(".cont-btn").click(function () 
      $(".cont-btn").removeClass("active");
      $(this).addClass("active");
      );


      Example






      share|improve this answer























      • Thank you very very very much, im a retard :facepalm:

        – Lolete Sape
        Nov 15 '18 at 3:04













      0












      0








      0







      There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:



      $(".cont-btn").click(function () 
      $(".cont-btn").removeClass("active");
      $(this).addClass("active");
      );


      Example






      share|improve this answer













      There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:



      $(".cont-btn").click(function () 
      $(".cont-btn").removeClass("active");
      $(this).addClass("active");
      );


      Example







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 15 '18 at 2:53









      Spencer WieczorekSpencer Wieczorek

      17.6k43345




      17.6k43345












      • Thank you very very very much, im a retard :facepalm:

        – Lolete Sape
        Nov 15 '18 at 3:04

















      • Thank you very very very much, im a retard :facepalm:

        – Lolete Sape
        Nov 15 '18 at 3:04
















      Thank you very very very much, im a retard :facepalm:

      – Lolete Sape
      Nov 15 '18 at 3:04





      Thank you very very very much, im a retard :facepalm:

      – Lolete Sape
      Nov 15 '18 at 3:04













      0














      You are making it more complicated than it needs to be.



      The following should do what you need assuming that <button> is inside class="cont-btn"



      var $buttons = $(".cont-btn button").click(function () 
      $buttons.removeClass('active');
      $(this).addClass('active');
      );





      share|improve this answer



























        0














        You are making it more complicated than it needs to be.



        The following should do what you need assuming that <button> is inside class="cont-btn"



        var $buttons = $(".cont-btn button").click(function () 
        $buttons.removeClass('active');
        $(this).addClass('active');
        );





        share|improve this answer

























          0












          0








          0







          You are making it more complicated than it needs to be.



          The following should do what you need assuming that <button> is inside class="cont-btn"



          var $buttons = $(".cont-btn button").click(function () 
          $buttons.removeClass('active');
          $(this).addClass('active');
          );





          share|improve this answer













          You are making it more complicated than it needs to be.



          The following should do what you need assuming that <button> is inside class="cont-btn"



          var $buttons = $(".cont-btn button").click(function () 
          $buttons.removeClass('active');
          $(this).addClass('active');
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 2:52









          charlietflcharlietfl

          140k1390124




          140k1390124





















              0














              $(".cont-btn button:nth-of-type(" + btnActive + ")") this code is executed only once.



              Your code may be like this



              $('.cont-btn button:eq(0)').addClass('active');
              $('.cont-btn button').click(function()
              $('.cont-btn button').removeClass('active');
              $(this).addClass('active');
              )


              P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.






              share|improve this answer



























                0














                $(".cont-btn button:nth-of-type(" + btnActive + ")") this code is executed only once.



                Your code may be like this



                $('.cont-btn button:eq(0)').addClass('active');
                $('.cont-btn button').click(function()
                $('.cont-btn button').removeClass('active');
                $(this).addClass('active');
                )


                P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.






                share|improve this answer

























                  0












                  0








                  0







                  $(".cont-btn button:nth-of-type(" + btnActive + ")") this code is executed only once.



                  Your code may be like this



                  $('.cont-btn button:eq(0)').addClass('active');
                  $('.cont-btn button').click(function()
                  $('.cont-btn button').removeClass('active');
                  $(this).addClass('active');
                  )


                  P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.






                  share|improve this answer













                  $(".cont-btn button:nth-of-type(" + btnActive + ")") this code is executed only once.



                  Your code may be like this



                  $('.cont-btn button:eq(0)').addClass('active');
                  $('.cont-btn button').click(function()
                  $('.cont-btn button').removeClass('active');
                  $(this).addClass('active');
                  )


                  P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 2:54









                  wanjaswanjas

                  30918




                  30918



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311678%2flocal-variable-in-a-function-to-global-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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo