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
  • 21,175
  • 15
  • 55
  • 84
Question User
  • 2,093
  • 3
  • 19
  • 29

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,594
  • 2
  • 21
  • 33
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
  • 21,175
  • 15
  • 55
  • 84
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
  • 76,418
  • 17
  • 112
  • 129