How to Skip Span Element Using jQuery Next Function












0















I have a panel which has to be like this



<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>


I need to target the dynamically added .prev by using jQuery next() function but looks like span is bugging this selection?~



Can you please let me know how to skip the span here?






$(document).on("click", ".next", function() {
$(this).next(".prev").addClass('red');
});

.red {
background: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>












share|improve this question


















  • 1





    If you know the span is always there, you could just do next().next('.prev') . The selector for the next operates as a filter. It doesn't make it go next until it finds a match. It filters out the next one if it does not match.

    – Taplar
    Nov 13 '18 at 19:11













  • I highly recommend reading Decoupling Your HTML, CSS, and JavaScript.

    – Erik Philips
    Nov 13 '18 at 19:27
















0















I have a panel which has to be like this



<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>


I need to target the dynamically added .prev by using jQuery next() function but looks like span is bugging this selection?~



Can you please let me know how to skip the span here?






$(document).on("click", ".next", function() {
$(this).next(".prev").addClass('red');
});

.red {
background: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>












share|improve this question


















  • 1





    If you know the span is always there, you could just do next().next('.prev') . The selector for the next operates as a filter. It doesn't make it go next until it finds a match. It filters out the next one if it does not match.

    – Taplar
    Nov 13 '18 at 19:11













  • I highly recommend reading Decoupling Your HTML, CSS, and JavaScript.

    – Erik Philips
    Nov 13 '18 at 19:27














0












0








0








I have a panel which has to be like this



<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>


I need to target the dynamically added .prev by using jQuery next() function but looks like span is bugging this selection?~



Can you please let me know how to skip the span here?






$(document).on("click", ".next", function() {
$(this).next(".prev").addClass('red');
});

.red {
background: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>












share|improve this question














I have a panel which has to be like this



<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>


I need to target the dynamically added .prev by using jQuery next() function but looks like span is bugging this selection?~



Can you please let me know how to skip the span here?






$(document).on("click", ".next", function() {
$(this).next(".prev").addClass('red');
});

.red {
background: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>








$(document).on("click", ".next", function() {
$(this).next(".prev").addClass('red');
});

.red {
background: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>





$(document).on("click", ".next", function() {
$(this).next(".prev").addClass('red');
});

.red {
background: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>






javascript jquery






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 19:09









Mona CoderMona Coder

2,409134083




2,409134083








  • 1





    If you know the span is always there, you could just do next().next('.prev') . The selector for the next operates as a filter. It doesn't make it go next until it finds a match. It filters out the next one if it does not match.

    – Taplar
    Nov 13 '18 at 19:11













  • I highly recommend reading Decoupling Your HTML, CSS, and JavaScript.

    – Erik Philips
    Nov 13 '18 at 19:27














  • 1





    If you know the span is always there, you could just do next().next('.prev') . The selector for the next operates as a filter. It doesn't make it go next until it finds a match. It filters out the next one if it does not match.

    – Taplar
    Nov 13 '18 at 19:11













  • I highly recommend reading Decoupling Your HTML, CSS, and JavaScript.

    – Erik Philips
    Nov 13 '18 at 19:27








1




1





If you know the span is always there, you could just do next().next('.prev') . The selector for the next operates as a filter. It doesn't make it go next until it finds a match. It filters out the next one if it does not match.

– Taplar
Nov 13 '18 at 19:11







If you know the span is always there, you could just do next().next('.prev') . The selector for the next operates as a filter. It doesn't make it go next until it finds a match. It filters out the next one if it does not match.

– Taplar
Nov 13 '18 at 19:11















I highly recommend reading Decoupling Your HTML, CSS, and JavaScript.

– Erik Philips
Nov 13 '18 at 19:27





I highly recommend reading Decoupling Your HTML, CSS, and JavaScript.

– Erik Philips
Nov 13 '18 at 19:27












3 Answers
3






active

oldest

votes


















2














To remove the positional based logic of your issue, I would suggest you encapsulate your elements and use parent() or closest() and perform a contextual lookup rather than using next() or prev().






$(document).on("click", ".next", function() {
$(this).closest('.navigation-wrapper').find('.prev').addClass('red');
});

.red {
background-color: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="navigation-wrapper">
<button class="next">Next</button>
<span>Test</span>
<button class="prev">Prev</button>
</span>





Using logic like this, you don't care if there is a span, or where it is. You know that the prev belongs to the wrapper.






share|improve this answer































    1














    You can use nextAll(".prev") to get all siblings of that class (.next() only fetches the immediate sibling), then isolate the first of that type with .eq(0)



    Like in this snippet!






    $(document).on("click", ".next", function() {
    $(this).nextAll(".prev").eq(0).addClass('red');
    });

    .red {
    background: red;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button class="next">Next</button>
    <span>Test</span>
    <button class="prev">Prev</button>








    share|improve this answer
























    • Again, this answer seems to accomplish exactly what OP requested, yet has also received a downvote with no explanation :/

      – Tyler Roper
      Nov 13 '18 at 19:24













    • I dv your answer, and uv your comment. The code, while answer the question explicity, is also extremely fragile in that it relies on the order of HTML for the functionality to work. For example, wrapping buttons causes your code not to work. See my comment to the question.

      – Erik Philips
      Nov 13 '18 at 19:28



















    0














    .next() only checks the next element. If you include a selector, it either returns the next element if it matches, or nothing.



    Consider using .siblings(".prev") (all siblings with class prev) combined with .first() (the first one only).






    $(document).on("click", ".next", function() {
    $(this).siblings(".prev").first().addClass('red');
    });

    .red {
    background: red;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button class="next">Next</button>
    <span>Test</span>
    <button class="prev">Prev</button>








    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%2f53287955%2fhow-to-skip-span-element-using-jquery-next-function%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









      2














      To remove the positional based logic of your issue, I would suggest you encapsulate your elements and use parent() or closest() and perform a contextual lookup rather than using next() or prev().






      $(document).on("click", ".next", function() {
      $(this).closest('.navigation-wrapper').find('.prev').addClass('red');
      });

      .red {
      background-color: red;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <span class="navigation-wrapper">
      <button class="next">Next</button>
      <span>Test</span>
      <button class="prev">Prev</button>
      </span>





      Using logic like this, you don't care if there is a span, or where it is. You know that the prev belongs to the wrapper.






      share|improve this answer




























        2














        To remove the positional based logic of your issue, I would suggest you encapsulate your elements and use parent() or closest() and perform a contextual lookup rather than using next() or prev().






        $(document).on("click", ".next", function() {
        $(this).closest('.navigation-wrapper').find('.prev').addClass('red');
        });

        .red {
        background-color: red;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <span class="navigation-wrapper">
        <button class="next">Next</button>
        <span>Test</span>
        <button class="prev">Prev</button>
        </span>





        Using logic like this, you don't care if there is a span, or where it is. You know that the prev belongs to the wrapper.






        share|improve this answer


























          2












          2








          2







          To remove the positional based logic of your issue, I would suggest you encapsulate your elements and use parent() or closest() and perform a contextual lookup rather than using next() or prev().






          $(document).on("click", ".next", function() {
          $(this).closest('.navigation-wrapper').find('.prev').addClass('red');
          });

          .red {
          background-color: red;
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <span class="navigation-wrapper">
          <button class="next">Next</button>
          <span>Test</span>
          <button class="prev">Prev</button>
          </span>





          Using logic like this, you don't care if there is a span, or where it is. You know that the prev belongs to the wrapper.






          share|improve this answer













          To remove the positional based logic of your issue, I would suggest you encapsulate your elements and use parent() or closest() and perform a contextual lookup rather than using next() or prev().






          $(document).on("click", ".next", function() {
          $(this).closest('.navigation-wrapper').find('.prev').addClass('red');
          });

          .red {
          background-color: red;
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <span class="navigation-wrapper">
          <button class="next">Next</button>
          <span>Test</span>
          <button class="prev">Prev</button>
          </span>





          Using logic like this, you don't care if there is a span, or where it is. You know that the prev belongs to the wrapper.






          $(document).on("click", ".next", function() {
          $(this).closest('.navigation-wrapper').find('.prev').addClass('red');
          });

          .red {
          background-color: red;
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <span class="navigation-wrapper">
          <button class="next">Next</button>
          <span>Test</span>
          <button class="prev">Prev</button>
          </span>





          $(document).on("click", ".next", function() {
          $(this).closest('.navigation-wrapper').find('.prev').addClass('red');
          });

          .red {
          background-color: red;
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <span class="navigation-wrapper">
          <button class="next">Next</button>
          <span>Test</span>
          <button class="prev">Prev</button>
          </span>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 19:19









          TaplarTaplar

          16k21529




          16k21529

























              1














              You can use nextAll(".prev") to get all siblings of that class (.next() only fetches the immediate sibling), then isolate the first of that type with .eq(0)



              Like in this snippet!






              $(document).on("click", ".next", function() {
              $(this).nextAll(".prev").eq(0).addClass('red');
              });

              .red {
              background: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <button class="next">Next</button>
              <span>Test</span>
              <button class="prev">Prev</button>








              share|improve this answer
























              • Again, this answer seems to accomplish exactly what OP requested, yet has also received a downvote with no explanation :/

                – Tyler Roper
                Nov 13 '18 at 19:24













              • I dv your answer, and uv your comment. The code, while answer the question explicity, is also extremely fragile in that it relies on the order of HTML for the functionality to work. For example, wrapping buttons causes your code not to work. See my comment to the question.

                – Erik Philips
                Nov 13 '18 at 19:28
















              1














              You can use nextAll(".prev") to get all siblings of that class (.next() only fetches the immediate sibling), then isolate the first of that type with .eq(0)



              Like in this snippet!






              $(document).on("click", ".next", function() {
              $(this).nextAll(".prev").eq(0).addClass('red');
              });

              .red {
              background: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <button class="next">Next</button>
              <span>Test</span>
              <button class="prev">Prev</button>








              share|improve this answer
























              • Again, this answer seems to accomplish exactly what OP requested, yet has also received a downvote with no explanation :/

                – Tyler Roper
                Nov 13 '18 at 19:24













              • I dv your answer, and uv your comment. The code, while answer the question explicity, is also extremely fragile in that it relies on the order of HTML for the functionality to work. For example, wrapping buttons causes your code not to work. See my comment to the question.

                – Erik Philips
                Nov 13 '18 at 19:28














              1












              1








              1







              You can use nextAll(".prev") to get all siblings of that class (.next() only fetches the immediate sibling), then isolate the first of that type with .eq(0)



              Like in this snippet!






              $(document).on("click", ".next", function() {
              $(this).nextAll(".prev").eq(0).addClass('red');
              });

              .red {
              background: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <button class="next">Next</button>
              <span>Test</span>
              <button class="prev">Prev</button>








              share|improve this answer













              You can use nextAll(".prev") to get all siblings of that class (.next() only fetches the immediate sibling), then isolate the first of that type with .eq(0)



              Like in this snippet!






              $(document).on("click", ".next", function() {
              $(this).nextAll(".prev").eq(0).addClass('red');
              });

              .red {
              background: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <button class="next">Next</button>
              <span>Test</span>
              <button class="prev">Prev</button>








              $(document).on("click", ".next", function() {
              $(this).nextAll(".prev").eq(0).addClass('red');
              });

              .red {
              background: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <button class="next">Next</button>
              <span>Test</span>
              <button class="prev">Prev</button>





              $(document).on("click", ".next", function() {
              $(this).nextAll(".prev").eq(0).addClass('red');
              });

              .red {
              background: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <button class="next">Next</button>
              <span>Test</span>
              <button class="prev">Prev</button>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 13 '18 at 19:22









              Michael BeesonMichael Beeson

              1,0942913




              1,0942913













              • Again, this answer seems to accomplish exactly what OP requested, yet has also received a downvote with no explanation :/

                – Tyler Roper
                Nov 13 '18 at 19:24













              • I dv your answer, and uv your comment. The code, while answer the question explicity, is also extremely fragile in that it relies on the order of HTML for the functionality to work. For example, wrapping buttons causes your code not to work. See my comment to the question.

                – Erik Philips
                Nov 13 '18 at 19:28



















              • Again, this answer seems to accomplish exactly what OP requested, yet has also received a downvote with no explanation :/

                – Tyler Roper
                Nov 13 '18 at 19:24













              • I dv your answer, and uv your comment. The code, while answer the question explicity, is also extremely fragile in that it relies on the order of HTML for the functionality to work. For example, wrapping buttons causes your code not to work. See my comment to the question.

                – Erik Philips
                Nov 13 '18 at 19:28

















              Again, this answer seems to accomplish exactly what OP requested, yet has also received a downvote with no explanation :/

              – Tyler Roper
              Nov 13 '18 at 19:24







              Again, this answer seems to accomplish exactly what OP requested, yet has also received a downvote with no explanation :/

              – Tyler Roper
              Nov 13 '18 at 19:24















              I dv your answer, and uv your comment. The code, while answer the question explicity, is also extremely fragile in that it relies on the order of HTML for the functionality to work. For example, wrapping buttons causes your code not to work. See my comment to the question.

              – Erik Philips
              Nov 13 '18 at 19:28





              I dv your answer, and uv your comment. The code, while answer the question explicity, is also extremely fragile in that it relies on the order of HTML for the functionality to work. For example, wrapping buttons causes your code not to work. See my comment to the question.

              – Erik Philips
              Nov 13 '18 at 19:28











              0














              .next() only checks the next element. If you include a selector, it either returns the next element if it matches, or nothing.



              Consider using .siblings(".prev") (all siblings with class prev) combined with .first() (the first one only).






              $(document).on("click", ".next", function() {
              $(this).siblings(".prev").first().addClass('red');
              });

              .red {
              background: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <button class="next">Next</button>
              <span>Test</span>
              <button class="prev">Prev</button>








              share|improve this answer




























                0














                .next() only checks the next element. If you include a selector, it either returns the next element if it matches, or nothing.



                Consider using .siblings(".prev") (all siblings with class prev) combined with .first() (the first one only).






                $(document).on("click", ".next", function() {
                $(this).siblings(".prev").first().addClass('red');
                });

                .red {
                background: red;
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <button class="next">Next</button>
                <span>Test</span>
                <button class="prev">Prev</button>








                share|improve this answer


























                  0












                  0








                  0







                  .next() only checks the next element. If you include a selector, it either returns the next element if it matches, or nothing.



                  Consider using .siblings(".prev") (all siblings with class prev) combined with .first() (the first one only).






                  $(document).on("click", ".next", function() {
                  $(this).siblings(".prev").first().addClass('red');
                  });

                  .red {
                  background: red;
                  }

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <button class="next">Next</button>
                  <span>Test</span>
                  <button class="prev">Prev</button>








                  share|improve this answer













                  .next() only checks the next element. If you include a selector, it either returns the next element if it matches, or nothing.



                  Consider using .siblings(".prev") (all siblings with class prev) combined with .first() (the first one only).






                  $(document).on("click", ".next", function() {
                  $(this).siblings(".prev").first().addClass('red');
                  });

                  .red {
                  background: red;
                  }

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <button class="next">Next</button>
                  <span>Test</span>
                  <button class="prev">Prev</button>








                  $(document).on("click", ".next", function() {
                  $(this).siblings(".prev").first().addClass('red');
                  });

                  .red {
                  background: red;
                  }

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <button class="next">Next</button>
                  <span>Test</span>
                  <button class="prev">Prev</button>





                  $(document).on("click", ".next", function() {
                  $(this).siblings(".prev").first().addClass('red');
                  });

                  .red {
                  background: red;
                  }

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <button class="next">Next</button>
                  <span>Test</span>
                  <button class="prev">Prev</button>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 19:12









                  Tyler RoperTyler Roper

                  13.6k31941




                  13.6k31941






























                      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%2f53287955%2fhow-to-skip-span-element-using-jquery-next-function%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

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values