[Solved] Adding Text to Text if Condition is met


To fetch the text you need this :

$("#data").find("input:checked").next("label").text();
     ^                ^                 ^        ^----------|
Where to look    What to find     Where to navigate    What to get

So basically, search in #data div for checked checkboxes and grab the
text of the label next to them.

Read :

  1. https://api.jquery.com/find/
  2. https://api.jquery.com/next/
  3. https://api.jquery.com/text/
$(function() {
  var prevText = $(".success").text();
  $("#submit-form").click(function() {
    var text = $("#data").find("input:checked").next("label").text();
    $(".success").text(prevText + " " + text).show();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="data">
  <input type="checkbox" id="foo">
  <label for="foo"> Foo Text</label>
  <input type="checkbox" id="bar">
  <label for="bar"> Bar Text</label>
</div>
<p class="success" style="display: none;">Thank you for</p>
<button type="submit" id="submit-form" class="btn btn-success pull-left">SUBSCRIBE</button>

0

solved Adding Text to Text if Condition is met