Hide element clicked, not the others with same class











up vote
0
down vote

favorite












I want to hide an element if 'this class' exists, but the parent shares the same class with other elements.



Basically, the HTML is:



<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>


If the button is clicked, it will add the following just to the element clicked:
<span class="confirm">Joined</span>



What I want to do know is, if class="confirm" exists, hide the input, just for the element clicked, not all of them



I was trying with:



$("#org-lists").each(function() {
$(this).find(".mailing-list").each(function() {
if($('.confirm').length) {
$('#org-lists .mailing-list').find('input[type="submit"]').hide();
}
});
});









share|improve this question






















  • is that confirmed span inside the mailing list
    – Pete
    Nov 22 at 11:33










  • @Pete yes is inside
    – john285
    Nov 22 at 11:37










  • Can you hide the input at the same time as the button is clicked? ie with the same code that you add the class?
    – freedomn-m
    Nov 22 at 11:40










  • @freedomn-m this is part of a widget and I can't modify/see its code
    – john285
    Nov 22 at 11:43















up vote
0
down vote

favorite












I want to hide an element if 'this class' exists, but the parent shares the same class with other elements.



Basically, the HTML is:



<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>


If the button is clicked, it will add the following just to the element clicked:
<span class="confirm">Joined</span>



What I want to do know is, if class="confirm" exists, hide the input, just for the element clicked, not all of them



I was trying with:



$("#org-lists").each(function() {
$(this).find(".mailing-list").each(function() {
if($('.confirm').length) {
$('#org-lists .mailing-list').find('input[type="submit"]').hide();
}
});
});









share|improve this question






















  • is that confirmed span inside the mailing list
    – Pete
    Nov 22 at 11:33










  • @Pete yes is inside
    – john285
    Nov 22 at 11:37










  • Can you hide the input at the same time as the button is clicked? ie with the same code that you add the class?
    – freedomn-m
    Nov 22 at 11:40










  • @freedomn-m this is part of a widget and I can't modify/see its code
    – john285
    Nov 22 at 11:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to hide an element if 'this class' exists, but the parent shares the same class with other elements.



Basically, the HTML is:



<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>


If the button is clicked, it will add the following just to the element clicked:
<span class="confirm">Joined</span>



What I want to do know is, if class="confirm" exists, hide the input, just for the element clicked, not all of them



I was trying with:



$("#org-lists").each(function() {
$(this).find(".mailing-list").each(function() {
if($('.confirm').length) {
$('#org-lists .mailing-list').find('input[type="submit"]').hide();
}
});
});









share|improve this question













I want to hide an element if 'this class' exists, but the parent shares the same class with other elements.



Basically, the HTML is:



<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>

<div class="mailing-list">
<input type="submit">
</div>


If the button is clicked, it will add the following just to the element clicked:
<span class="confirm">Joined</span>



What I want to do know is, if class="confirm" exists, hide the input, just for the element clicked, not all of them



I was trying with:



$("#org-lists").each(function() {
$(this).find(".mailing-list").each(function() {
if($('.confirm').length) {
$('#org-lists .mailing-list').find('input[type="submit"]').hide();
}
});
});






javascript jquery html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 11:31









john285

295




295












  • is that confirmed span inside the mailing list
    – Pete
    Nov 22 at 11:33










  • @Pete yes is inside
    – john285
    Nov 22 at 11:37










  • Can you hide the input at the same time as the button is clicked? ie with the same code that you add the class?
    – freedomn-m
    Nov 22 at 11:40










  • @freedomn-m this is part of a widget and I can't modify/see its code
    – john285
    Nov 22 at 11:43


















  • is that confirmed span inside the mailing list
    – Pete
    Nov 22 at 11:33










  • @Pete yes is inside
    – john285
    Nov 22 at 11:37










  • Can you hide the input at the same time as the button is clicked? ie with the same code that you add the class?
    – freedomn-m
    Nov 22 at 11:40










  • @freedomn-m this is part of a widget and I can't modify/see its code
    – john285
    Nov 22 at 11:43
















is that confirmed span inside the mailing list
– Pete
Nov 22 at 11:33




is that confirmed span inside the mailing list
– Pete
Nov 22 at 11:33












@Pete yes is inside
– john285
Nov 22 at 11:37




@Pete yes is inside
– john285
Nov 22 at 11:37












Can you hide the input at the same time as the button is clicked? ie with the same code that you add the class?
– freedomn-m
Nov 22 at 11:40




Can you hide the input at the same time as the button is clicked? ie with the same code that you add the class?
– freedomn-m
Nov 22 at 11:40












@freedomn-m this is part of a widget and I can't modify/see its code
– john285
Nov 22 at 11:43




@freedomn-m this is part of a widget and I can't modify/see its code
– john285
Nov 22 at 11:43












5 Answers
5






active

oldest

votes

















up vote
0
down vote



accepted










In addition to existing answers, you can also use :has https://api.jquery.com/has-selector/



$('.mailing-list:has(.confirm) input[type="submit"]').hide();


To fix your original code, you need to add this to give:



$("#org-lists").each(function() {
// this = org-list
$(this).find(".mailing-list").each(function() {
// this = mailing-list
// find if this mailing-list has a confirm (need >0 check)
if ($(this).find('.confirm').length > 0) {
// this still = mailing-list
$(this).find('input[type="submit"]').hide();
}
});
});





share|improve this answer




























    up vote
    1
    down vote













    hide the input element and append the span tag to its parent class






    $('input').click(function(){
    $(this).hide();
    $(this).parent().append('<span class="confirm">Joined</span>');
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="mailing-list">
    <input type="submit">
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>








    share|improve this answer





















    • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
      – john285
      Nov 22 at 11:42


















    up vote
    1
    down vote













    You need to bind it to the click event:






    $('.mailing-list > input').on('click', function() {
    var $input = $(this),
    $parent = $input.parent(); // this is the mailing list

    $parent.append('<span class="confirm">Joined</span>'); // add joined span
    $input.hide(); // hide input
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="mailing-list">
    <input type="submit">
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>





    If you want to do it in a loop separately from the click event then you can use a filter:






    $('.mailing-list').filter(function() {
    return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
    })
    .children('input') // get the inputs of the filtered
    .hide(); // hide them

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="mailing-list">
    <input type="submit">
    <span class="confirm">Joined</span>
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>





    Sounds like you need to observe the DOM to run the above loop if you can't bind it to a click:






    $('.mailing-list > input').on('click', function() {  
    $(this).parent().append('<span class="confirm">Joined</span>'); // add joined span - this is other code not important
    });

    // Select the node that will be observed for mutations
    var targetNodes = document.querySelectorAll('.mailing-list');

    // Options for the observer (which mutations to observe)
    var config = { attributes: false, childList: true, subtree: false };

    // Callback function to execute when mutations are observed
    var callback = function(mutationsList, observer) {
    for(var mutation of mutationsList) {
    if (mutation.type == 'childList') {
    $('.mailing-list').filter(function() {
    return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
    })
    .children('input') // get the inputs of the filtered
    .hide(); // hide them
    }
    }
    };

    // Create an observer instance linked to the callback function
    var observer = new MutationObserver(callback);

    // watch the mailing list divs
    for (i = 0; i < targetNodes.length; i++) {
    observer.observe(targetNodes[i], config);
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="mailing-list">
    <input type="submit">
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>

    <div class="mailing-list">
    <input type="submit">
    </div>








    share|improve this answer























    • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
      – john285
      Nov 22 at 11:42










    • see edit above - second snippet just uses a loop
      – Pete
      Nov 22 at 11:43










    • Not working I'm afraid
      – john285
      Nov 22 at 11:46










    • That's probably because you run it on ready and not after the element is clicked
      – Pete
      Nov 22 at 11:46










    • How can I run it after it's clicked?
      – john285
      Nov 22 at 11:47


















    up vote
    0
    down vote













    This is the solution required, @ssamuel code used here.






      $(document).on('click', '.mailing-list', function () {
    $(this).find("input").hide();
    $(this).parent().append('<span class="confirm">Joined</span>');
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="mailing-list">
    <input type="submit" value="1">
    </div>

    <div class="mailing-list">
    <input type="submit" value="2">
    </div>

    <div class="mailing-list">
    <input type="submit" value="3">
    </div>








    share|improve this answer




























      up vote
      0
      down vote













      Do a loop over each .confirm element, select the input (by doing up to the parent) and hide it



      $('.confirm').each(function(){
      $(this).parent().find('input[type="submit"]').hide();
      });


      you will need to call this after you add your .confirm elements, better if you call the hide when you append .confirm like



      $('input[type="submit"]').on('submit',function(e){
      e.preventDefault();
      $(this).parent().append('<span class="confirm">Joined</span>');
      $(this).hide();
      //other code here
      })


      another option will be to create a interval and call the function on a specific time



      setInterval(function(){  $('.confirm').each(function(){
      $(this).parent().find('input[type="submit"]').hide();
      }); }, 30);





      share|improve this answer























      • but that confirm is just added after the click. So how can it say: if confirm exists, then hide submit
        – john285
        Nov 22 at 11:39










      • then you don't need the loop just place the hide code in the click event
        – madalinivascu
        Nov 22 at 11:39










      • @john285 see my updated answer
        – madalinivascu
        Nov 22 at 11:43










      • "how can it say if confirm exists" - that's what $('.confirm').each does - it only runs the inner part when confirm exists.
        – freedomn-m
        Nov 22 at 11:52










      • updated my answer
        – madalinivascu
        Nov 22 at 12:05











      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%2f53430015%2fhide-element-clicked-not-the-others-with-same-class%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      In addition to existing answers, you can also use :has https://api.jquery.com/has-selector/



      $('.mailing-list:has(.confirm) input[type="submit"]').hide();


      To fix your original code, you need to add this to give:



      $("#org-lists").each(function() {
      // this = org-list
      $(this).find(".mailing-list").each(function() {
      // this = mailing-list
      // find if this mailing-list has a confirm (need >0 check)
      if ($(this).find('.confirm').length > 0) {
      // this still = mailing-list
      $(this).find('input[type="submit"]').hide();
      }
      });
      });





      share|improve this answer

























        up vote
        0
        down vote



        accepted










        In addition to existing answers, you can also use :has https://api.jquery.com/has-selector/



        $('.mailing-list:has(.confirm) input[type="submit"]').hide();


        To fix your original code, you need to add this to give:



        $("#org-lists").each(function() {
        // this = org-list
        $(this).find(".mailing-list").each(function() {
        // this = mailing-list
        // find if this mailing-list has a confirm (need >0 check)
        if ($(this).find('.confirm').length > 0) {
        // this still = mailing-list
        $(this).find('input[type="submit"]').hide();
        }
        });
        });





        share|improve this answer























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          In addition to existing answers, you can also use :has https://api.jquery.com/has-selector/



          $('.mailing-list:has(.confirm) input[type="submit"]').hide();


          To fix your original code, you need to add this to give:



          $("#org-lists").each(function() {
          // this = org-list
          $(this).find(".mailing-list").each(function() {
          // this = mailing-list
          // find if this mailing-list has a confirm (need >0 check)
          if ($(this).find('.confirm').length > 0) {
          // this still = mailing-list
          $(this).find('input[type="submit"]').hide();
          }
          });
          });





          share|improve this answer












          In addition to existing answers, you can also use :has https://api.jquery.com/has-selector/



          $('.mailing-list:has(.confirm) input[type="submit"]').hide();


          To fix your original code, you need to add this to give:



          $("#org-lists").each(function() {
          // this = org-list
          $(this).find(".mailing-list").each(function() {
          // this = mailing-list
          // find if this mailing-list has a confirm (need >0 check)
          if ($(this).find('.confirm').length > 0) {
          // this still = mailing-list
          $(this).find('input[type="submit"]').hide();
          }
          });
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 11:48









          freedomn-m

          11.8k31841




          11.8k31841
























              up vote
              1
              down vote













              hide the input element and append the span tag to its parent class






              $('input').click(function(){
              $(this).hide();
              $(this).parent().append('<span class="confirm">Joined</span>');
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              share|improve this answer





















              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42















              up vote
              1
              down vote













              hide the input element and append the span tag to its parent class






              $('input').click(function(){
              $(this).hide();
              $(this).parent().append('<span class="confirm">Joined</span>');
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              share|improve this answer





















              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42













              up vote
              1
              down vote










              up vote
              1
              down vote









              hide the input element and append the span tag to its parent class






              $('input').click(function(){
              $(this).hide();
              $(this).parent().append('<span class="confirm">Joined</span>');
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              share|improve this answer












              hide the input element and append the span tag to its parent class






              $('input').click(function(){
              $(this).hide();
              $(this).parent().append('<span class="confirm">Joined</span>');
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              $('input').click(function(){
              $(this).hide();
              $(this).parent().append('<span class="confirm">Joined</span>');
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              $('input').click(function(){
              $(this).hide();
              $(this).parent().append('<span class="confirm">Joined</span>');
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 at 11:38









              ssamuel

              2646




              2646












              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42


















              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42
















              Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
              – john285
              Nov 22 at 11:42




              Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
              – john285
              Nov 22 at 11:42










              up vote
              1
              down vote













              You need to bind it to the click event:






              $('.mailing-list > input').on('click', function() {
              var $input = $(this),
              $parent = $input.parent(); // this is the mailing list

              $parent.append('<span class="confirm">Joined</span>'); // add joined span
              $input.hide(); // hide input
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              If you want to do it in a loop separately from the click event then you can use a filter:






              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              <span class="confirm">Joined</span>
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              Sounds like you need to observe the DOM to run the above loop if you can't bind it to a click:






              $('.mailing-list > input').on('click', function() {  
              $(this).parent().append('<span class="confirm">Joined</span>'); // add joined span - this is other code not important
              });

              // Select the node that will be observed for mutations
              var targetNodes = document.querySelectorAll('.mailing-list');

              // Options for the observer (which mutations to observe)
              var config = { attributes: false, childList: true, subtree: false };

              // Callback function to execute when mutations are observed
              var callback = function(mutationsList, observer) {
              for(var mutation of mutationsList) {
              if (mutation.type == 'childList') {
              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them
              }
              }
              };

              // Create an observer instance linked to the callback function
              var observer = new MutationObserver(callback);

              // watch the mailing list divs
              for (i = 0; i < targetNodes.length; i++) {
              observer.observe(targetNodes[i], config);
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              share|improve this answer























              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42










              • see edit above - second snippet just uses a loop
                – Pete
                Nov 22 at 11:43










              • Not working I'm afraid
                – john285
                Nov 22 at 11:46










              • That's probably because you run it on ready and not after the element is clicked
                – Pete
                Nov 22 at 11:46










              • How can I run it after it's clicked?
                – john285
                Nov 22 at 11:47















              up vote
              1
              down vote













              You need to bind it to the click event:






              $('.mailing-list > input').on('click', function() {
              var $input = $(this),
              $parent = $input.parent(); // this is the mailing list

              $parent.append('<span class="confirm">Joined</span>'); // add joined span
              $input.hide(); // hide input
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              If you want to do it in a loop separately from the click event then you can use a filter:






              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              <span class="confirm">Joined</span>
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              Sounds like you need to observe the DOM to run the above loop if you can't bind it to a click:






              $('.mailing-list > input').on('click', function() {  
              $(this).parent().append('<span class="confirm">Joined</span>'); // add joined span - this is other code not important
              });

              // Select the node that will be observed for mutations
              var targetNodes = document.querySelectorAll('.mailing-list');

              // Options for the observer (which mutations to observe)
              var config = { attributes: false, childList: true, subtree: false };

              // Callback function to execute when mutations are observed
              var callback = function(mutationsList, observer) {
              for(var mutation of mutationsList) {
              if (mutation.type == 'childList') {
              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them
              }
              }
              };

              // Create an observer instance linked to the callback function
              var observer = new MutationObserver(callback);

              // watch the mailing list divs
              for (i = 0; i < targetNodes.length; i++) {
              observer.observe(targetNodes[i], config);
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              share|improve this answer























              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42










              • see edit above - second snippet just uses a loop
                – Pete
                Nov 22 at 11:43










              • Not working I'm afraid
                – john285
                Nov 22 at 11:46










              • That's probably because you run it on ready and not after the element is clicked
                – Pete
                Nov 22 at 11:46










              • How can I run it after it's clicked?
                – john285
                Nov 22 at 11:47













              up vote
              1
              down vote










              up vote
              1
              down vote









              You need to bind it to the click event:






              $('.mailing-list > input').on('click', function() {
              var $input = $(this),
              $parent = $input.parent(); // this is the mailing list

              $parent.append('<span class="confirm">Joined</span>'); // add joined span
              $input.hide(); // hide input
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              If you want to do it in a loop separately from the click event then you can use a filter:






              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              <span class="confirm">Joined</span>
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              Sounds like you need to observe the DOM to run the above loop if you can't bind it to a click:






              $('.mailing-list > input').on('click', function() {  
              $(this).parent().append('<span class="confirm">Joined</span>'); // add joined span - this is other code not important
              });

              // Select the node that will be observed for mutations
              var targetNodes = document.querySelectorAll('.mailing-list');

              // Options for the observer (which mutations to observe)
              var config = { attributes: false, childList: true, subtree: false };

              // Callback function to execute when mutations are observed
              var callback = function(mutationsList, observer) {
              for(var mutation of mutationsList) {
              if (mutation.type == 'childList') {
              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them
              }
              }
              };

              // Create an observer instance linked to the callback function
              var observer = new MutationObserver(callback);

              // watch the mailing list divs
              for (i = 0; i < targetNodes.length; i++) {
              observer.observe(targetNodes[i], config);
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              share|improve this answer














              You need to bind it to the click event:






              $('.mailing-list > input').on('click', function() {
              var $input = $(this),
              $parent = $input.parent(); // this is the mailing list

              $parent.append('<span class="confirm">Joined</span>'); // add joined span
              $input.hide(); // hide input
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              If you want to do it in a loop separately from the click event then you can use a filter:






              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              <span class="confirm">Joined</span>
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              Sounds like you need to observe the DOM to run the above loop if you can't bind it to a click:






              $('.mailing-list > input').on('click', function() {  
              $(this).parent().append('<span class="confirm">Joined</span>'); // add joined span - this is other code not important
              });

              // Select the node that will be observed for mutations
              var targetNodes = document.querySelectorAll('.mailing-list');

              // Options for the observer (which mutations to observe)
              var config = { attributes: false, childList: true, subtree: false };

              // Callback function to execute when mutations are observed
              var callback = function(mutationsList, observer) {
              for(var mutation of mutationsList) {
              if (mutation.type == 'childList') {
              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them
              }
              }
              };

              // Create an observer instance linked to the callback function
              var observer = new MutationObserver(callback);

              // watch the mailing list divs
              for (i = 0; i < targetNodes.length; i++) {
              observer.observe(targetNodes[i], config);
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>








              $('.mailing-list > input').on('click', function() {
              var $input = $(this),
              $parent = $input.parent(); // this is the mailing list

              $parent.append('<span class="confirm">Joined</span>'); // add joined span
              $input.hide(); // hide input
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              $('.mailing-list > input').on('click', function() {
              var $input = $(this),
              $parent = $input.parent(); // this is the mailing list

              $parent.append('<span class="confirm">Joined</span>'); // add joined span
              $input.hide(); // hide input
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              <span class="confirm">Joined</span>
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              <span class="confirm">Joined</span>
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              $('.mailing-list > input').on('click', function() {  
              $(this).parent().append('<span class="confirm">Joined</span>'); // add joined span - this is other code not important
              });

              // Select the node that will be observed for mutations
              var targetNodes = document.querySelectorAll('.mailing-list');

              // Options for the observer (which mutations to observe)
              var config = { attributes: false, childList: true, subtree: false };

              // Callback function to execute when mutations are observed
              var callback = function(mutationsList, observer) {
              for(var mutation of mutationsList) {
              if (mutation.type == 'childList') {
              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them
              }
              }
              };

              // Create an observer instance linked to the callback function
              var observer = new MutationObserver(callback);

              // watch the mailing list divs
              for (i = 0; i < targetNodes.length; i++) {
              observer.observe(targetNodes[i], config);
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>





              $('.mailing-list > input').on('click', function() {  
              $(this).parent().append('<span class="confirm">Joined</span>'); // add joined span - this is other code not important
              });

              // Select the node that will be observed for mutations
              var targetNodes = document.querySelectorAll('.mailing-list');

              // Options for the observer (which mutations to observe)
              var config = { attributes: false, childList: true, subtree: false };

              // Callback function to execute when mutations are observed
              var callback = function(mutationsList, observer) {
              for(var mutation of mutationsList) {
              if (mutation.type == 'childList') {
              $('.mailing-list').filter(function() {
              return $(this).children('.confirm').length; // filter any mailing lists with a child of confirm
              })
              .children('input') // get the inputs of the filtered
              .hide(); // hide them
              }
              }
              };

              // Create an observer instance linked to the callback function
              var observer = new MutationObserver(callback);

              // watch the mailing list divs
              for (i = 0; i < targetNodes.length; i++) {
              observer.observe(targetNodes[i], config);
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>

              <div class="mailing-list">
              <input type="submit">
              </div>






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 at 12:00

























              answered Nov 22 at 11:36









              Pete

              40.4k1875116




              40.4k1875116












              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42










              • see edit above - second snippet just uses a loop
                – Pete
                Nov 22 at 11:43










              • Not working I'm afraid
                – john285
                Nov 22 at 11:46










              • That's probably because you run it on ready and not after the element is clicked
                – Pete
                Nov 22 at 11:46










              • How can I run it after it's clicked?
                – john285
                Nov 22 at 11:47


















              • Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
                – john285
                Nov 22 at 11:42










              • see edit above - second snippet just uses a loop
                – Pete
                Nov 22 at 11:43










              • Not working I'm afraid
                – john285
                Nov 22 at 11:46










              • That's probably because you run it on ready and not after the element is clicked
                – Pete
                Nov 22 at 11:46










              • How can I run it after it's clicked?
                – john285
                Nov 22 at 11:47
















              Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
              – john285
              Nov 22 at 11:42




              Thanks. The thing is that I can't use click function because that stops another function from running. So it should be done with length or something similar
              – john285
              Nov 22 at 11:42












              see edit above - second snippet just uses a loop
              – Pete
              Nov 22 at 11:43




              see edit above - second snippet just uses a loop
              – Pete
              Nov 22 at 11:43












              Not working I'm afraid
              – john285
              Nov 22 at 11:46




              Not working I'm afraid
              – john285
              Nov 22 at 11:46












              That's probably because you run it on ready and not after the element is clicked
              – Pete
              Nov 22 at 11:46




              That's probably because you run it on ready and not after the element is clicked
              – Pete
              Nov 22 at 11:46












              How can I run it after it's clicked?
              – john285
              Nov 22 at 11:47




              How can I run it after it's clicked?
              – john285
              Nov 22 at 11:47










              up vote
              0
              down vote













              This is the solution required, @ssamuel code used here.






                $(document).on('click', '.mailing-list', function () {
              $(this).find("input").hide();
              $(this).parent().append('<span class="confirm">Joined</span>');
              });

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="mailing-list">
              <input type="submit" value="1">
              </div>

              <div class="mailing-list">
              <input type="submit" value="2">
              </div>

              <div class="mailing-list">
              <input type="submit" value="3">
              </div>








              share|improve this answer

























                up vote
                0
                down vote













                This is the solution required, @ssamuel code used here.






                  $(document).on('click', '.mailing-list', function () {
                $(this).find("input").hide();
                $(this).parent().append('<span class="confirm">Joined</span>');
                });

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <div class="mailing-list">
                <input type="submit" value="1">
                </div>

                <div class="mailing-list">
                <input type="submit" value="2">
                </div>

                <div class="mailing-list">
                <input type="submit" value="3">
                </div>








                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This is the solution required, @ssamuel code used here.






                    $(document).on('click', '.mailing-list', function () {
                  $(this).find("input").hide();
                  $(this).parent().append('<span class="confirm">Joined</span>');
                  });

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <div class="mailing-list">
                  <input type="submit" value="1">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="2">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="3">
                  </div>








                  share|improve this answer












                  This is the solution required, @ssamuel code used here.






                    $(document).on('click', '.mailing-list', function () {
                  $(this).find("input").hide();
                  $(this).parent().append('<span class="confirm">Joined</span>');
                  });

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <div class="mailing-list">
                  <input type="submit" value="1">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="2">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="3">
                  </div>








                    $(document).on('click', '.mailing-list', function () {
                  $(this).find("input").hide();
                  $(this).parent().append('<span class="confirm">Joined</span>');
                  });

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <div class="mailing-list">
                  <input type="submit" value="1">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="2">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="3">
                  </div>





                    $(document).on('click', '.mailing-list', function () {
                  $(this).find("input").hide();
                  $(this).parent().append('<span class="confirm">Joined</span>');
                  });

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <div class="mailing-list">
                  <input type="submit" value="1">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="2">
                  </div>

                  <div class="mailing-list">
                  <input type="submit" value="3">
                  </div>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 12:00









                  Pranesh Janarthanan

                  457516




                  457516






















                      up vote
                      0
                      down vote













                      Do a loop over each .confirm element, select the input (by doing up to the parent) and hide it



                      $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      });


                      you will need to call this after you add your .confirm elements, better if you call the hide when you append .confirm like



                      $('input[type="submit"]').on('submit',function(e){
                      e.preventDefault();
                      $(this).parent().append('<span class="confirm">Joined</span>');
                      $(this).hide();
                      //other code here
                      })


                      another option will be to create a interval and call the function on a specific time



                      setInterval(function(){  $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      }); }, 30);





                      share|improve this answer























                      • but that confirm is just added after the click. So how can it say: if confirm exists, then hide submit
                        – john285
                        Nov 22 at 11:39










                      • then you don't need the loop just place the hide code in the click event
                        – madalinivascu
                        Nov 22 at 11:39










                      • @john285 see my updated answer
                        – madalinivascu
                        Nov 22 at 11:43










                      • "how can it say if confirm exists" - that's what $('.confirm').each does - it only runs the inner part when confirm exists.
                        – freedomn-m
                        Nov 22 at 11:52










                      • updated my answer
                        – madalinivascu
                        Nov 22 at 12:05















                      up vote
                      0
                      down vote













                      Do a loop over each .confirm element, select the input (by doing up to the parent) and hide it



                      $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      });


                      you will need to call this after you add your .confirm elements, better if you call the hide when you append .confirm like



                      $('input[type="submit"]').on('submit',function(e){
                      e.preventDefault();
                      $(this).parent().append('<span class="confirm">Joined</span>');
                      $(this).hide();
                      //other code here
                      })


                      another option will be to create a interval and call the function on a specific time



                      setInterval(function(){  $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      }); }, 30);





                      share|improve this answer























                      • but that confirm is just added after the click. So how can it say: if confirm exists, then hide submit
                        – john285
                        Nov 22 at 11:39










                      • then you don't need the loop just place the hide code in the click event
                        – madalinivascu
                        Nov 22 at 11:39










                      • @john285 see my updated answer
                        – madalinivascu
                        Nov 22 at 11:43










                      • "how can it say if confirm exists" - that's what $('.confirm').each does - it only runs the inner part when confirm exists.
                        – freedomn-m
                        Nov 22 at 11:52










                      • updated my answer
                        – madalinivascu
                        Nov 22 at 12:05













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Do a loop over each .confirm element, select the input (by doing up to the parent) and hide it



                      $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      });


                      you will need to call this after you add your .confirm elements, better if you call the hide when you append .confirm like



                      $('input[type="submit"]').on('submit',function(e){
                      e.preventDefault();
                      $(this).parent().append('<span class="confirm">Joined</span>');
                      $(this).hide();
                      //other code here
                      })


                      another option will be to create a interval and call the function on a specific time



                      setInterval(function(){  $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      }); }, 30);





                      share|improve this answer














                      Do a loop over each .confirm element, select the input (by doing up to the parent) and hide it



                      $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      });


                      you will need to call this after you add your .confirm elements, better if you call the hide when you append .confirm like



                      $('input[type="submit"]').on('submit',function(e){
                      e.preventDefault();
                      $(this).parent().append('<span class="confirm">Joined</span>');
                      $(this).hide();
                      //other code here
                      })


                      another option will be to create a interval and call the function on a specific time



                      setInterval(function(){  $('.confirm').each(function(){
                      $(this).parent().find('input[type="submit"]').hide();
                      }); }, 30);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 at 12:05

























                      answered Nov 22 at 11:34









                      madalinivascu

                      26.8k21841




                      26.8k21841












                      • but that confirm is just added after the click. So how can it say: if confirm exists, then hide submit
                        – john285
                        Nov 22 at 11:39










                      • then you don't need the loop just place the hide code in the click event
                        – madalinivascu
                        Nov 22 at 11:39










                      • @john285 see my updated answer
                        – madalinivascu
                        Nov 22 at 11:43










                      • "how can it say if confirm exists" - that's what $('.confirm').each does - it only runs the inner part when confirm exists.
                        – freedomn-m
                        Nov 22 at 11:52










                      • updated my answer
                        – madalinivascu
                        Nov 22 at 12:05


















                      • but that confirm is just added after the click. So how can it say: if confirm exists, then hide submit
                        – john285
                        Nov 22 at 11:39










                      • then you don't need the loop just place the hide code in the click event
                        – madalinivascu
                        Nov 22 at 11:39










                      • @john285 see my updated answer
                        – madalinivascu
                        Nov 22 at 11:43










                      • "how can it say if confirm exists" - that's what $('.confirm').each does - it only runs the inner part when confirm exists.
                        – freedomn-m
                        Nov 22 at 11:52










                      • updated my answer
                        – madalinivascu
                        Nov 22 at 12:05
















                      but that confirm is just added after the click. So how can it say: if confirm exists, then hide submit
                      – john285
                      Nov 22 at 11:39




                      but that confirm is just added after the click. So how can it say: if confirm exists, then hide submit
                      – john285
                      Nov 22 at 11:39












                      then you don't need the loop just place the hide code in the click event
                      – madalinivascu
                      Nov 22 at 11:39




                      then you don't need the loop just place the hide code in the click event
                      – madalinivascu
                      Nov 22 at 11:39












                      @john285 see my updated answer
                      – madalinivascu
                      Nov 22 at 11:43




                      @john285 see my updated answer
                      – madalinivascu
                      Nov 22 at 11:43












                      "how can it say if confirm exists" - that's what $('.confirm').each does - it only runs the inner part when confirm exists.
                      – freedomn-m
                      Nov 22 at 11:52




                      "how can it say if confirm exists" - that's what $('.confirm').each does - it only runs the inner part when confirm exists.
                      – freedomn-m
                      Nov 22 at 11:52












                      updated my answer
                      – madalinivascu
                      Nov 22 at 12:05




                      updated my answer
                      – madalinivascu
                      Nov 22 at 12:05


















                      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%2f53430015%2fhide-element-clicked-not-the-others-with-same-class%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

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)