hmahdavi
hmahdavi

Reputation: 2354

How to change text of label that contains input tag?

I want change records per page text but when I change text of label : the select tag deleted. Please advice.

  <div id="dynamic-table_filter" class="dataTables_filter">
        <label>Search:    
            <input aria-controls="dynamic-table" type="text">
        </label>
  </div>

jquery :

$('#dynamic-table_filter label').text('جستجو');

Upvotes: 3

Views: 812

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115222

Get the textNode and replace it with new text. Using contents() get all children including text and comment node, since textNode is the first child node it can be get by eq(0) then replace content using replaceWith().

$('#dynamic-table_filter label').contents().eq(0).replaceWith('جستجو');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="dynamic-table_filter" class="dataTables_filter">
  <label>Search:
    <input aria-controls="dynamic-table" type="text">
  </label>
</div>


Using pure javascript

document.querySelector('#dynamic-table_filter label').firstChild.nodeValue = 'جستجو';
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="dynamic-table_filter" class="dataTables_filter">
  <label>Search:
    <input aria-controls="dynamic-table" type="text">
  </label>
</div>

Upvotes: 3

Related Questions