0

How can I remove the text after the input in jQuery?

jQuery

<div id="date_filter_si">
    <input type="radio" class="date_action" name="date_action" value="Y" checked=""> year
    <input type="radio" class="date_action" name="date_action" value="W" checked=""> Weekly
</div>

Expected Output

<div id="date_filter_si">
    <input type="radio" class="date_action" name="date_action" value="Y" checked=""> 
    <input type="radio" class="date_action" name="date_action" value="W" checked=""> 
</div>
$( "#date_filter_si:contains('Weekly')" ).remove();
Mohammad
  • 20,339
  • 15
  • 51
  • 79
Question User
  • 1,741
  • 2
  • 17
  • 28

3 Answers3

2

You can filter the text nodes (element.nodeType == 3) and then remove.

$("#date_filter_si").contents().filter(function() {
    return this.nodeType === 3; 
}).remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">  
    <input type="radio" class="date_action" name="date_action" value="Y" checked="" onclick="set_date_filter('Y')"> year
    <input type="radio" class="date_action" name="date_action" value="W" checked="" onclick="set_date_filter('W')"> Weekly
</div>
Deep
  • 9,344
  • 2
  • 17
  • 31
2

You can use Node.nextSibling property to change next sibling text of element.

$("#date_filter_si > input")[0].nextSibling.textContent = "";

$("#date_filter_si > input")[0].nextSibling.textContent = "";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
  <input type="radio" class="date_action"> year
</div>

If you have multiple input and sibling text use this:

$("#date_filter_si > input").each(function(){
    this.nextSibling.textContent = "";
});

$("#date_filter_si > input").each(function(){
  this.nextSibling.textContent = "";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
  <input type="radio" class="date_action"> year
  <input type="radio" class="date_action"> week
</div>
Mohammad
  • 20,339
  • 15
  • 51
  • 79
0

This is how I would do it.

$( "#date_filter_si:contains('Weekly') input" ).each(function(){
  var el = this.nextSibling;
  if(el && el.nodeType === Node.TEXT_NODE) {
    el.parentNode.removeChild(el);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="date_filter_si">
    <input type="radio" class="date_action" name="date_action" value="Y" checked="" onclick="set_date_filter('Y')"> year
    <input type="radio" class="date_action" name="date_action" value="W" checked="" onclick="set_date_filter('W')"> Weekly
</div>
Joseph Marikle
  • 72,900
  • 16
  • 109
  • 126