2

I have some HTML:

<label>Search:
  <input id="search-box" type="text"/>
</label>

If I want to change the words "Search:" to "Quick Search:", how can I select just the "Search:" text using jQuery/JS?

I have tried html(), text(), and other various jQuery methods. My goal is to NOT have to copy the input box and then append it after. Is it possible?

P.S. I know I can use the prepend() function, but my goal here is to replace that text entirely, and I'd like to know how to do this in the future as well.

Thanks a bunch.

dmcmulle
  • 339
  • 3
  • 11

8 Answers8

3

If you have access to the HTML I'd wrap the text in a span so that you can address it easily. However, with what you have so far you can simply iterate over all nodes in the element and change the textContent of the first text node (nodeType==3):

$(function(){
    
    $("label").contents().each(function() {
        if(this.nodeType == 3){
            this.textContent = "Quick Search: ";
            return false;
        }
    })
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Search:
  <input id="search-box" type="text"/>
</label>
Moob
  • 14,420
  • 1
  • 34
  • 47
3

To do it you would need to look at the text nodes. You get them by using contents(). This solution below is a little overkill, but this shows you how to look for a textNode (type 3) and look to see if the text has Search in it.

var textNodes = $("label")
  .contents()
  .filter(function() {
    return this.nodeType === 3 && this.nodeValue.indexOf("Search")>-1; 
  }).each( function() { this.nodeValue = this.nodeValue.replace("Search", "Quick Search"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>Search:
  <input id="search-box" type="text"/>
</label>

You can get rid of the filter and just do it all in the each and break out if you want.

var textNodes = $("label")  //should use a better selector
  .contents()
  .each(function() {
      if( this.nodeType === 3 && this.nodeValue.indexOf("Search")>-1 ) {   
          this.nodeValue = this.nodeValue.replace("Search", "Quick Search");
          return false; 
      }
  );
epascarello
  • 204,599
  • 20
  • 195
  • 236
3

You can do this with straight Javascript...no jQuery needed. You just need to access the first node in the label and change the value.

document.querySelectorAll('label')[0].firstChild.nodeValue = "Quicksearch: ";
<label>Search:
  <input id="search-box" type="text"/>
</label>
Jeff
  • 4,136
  • 23
  • 32
  • Clearly far superior to my own answer. I'm all for jQuery-free solutions. (N.b. nit-pickers might want you to change 'Quicksearch: ' to 'Quick Search: ') – Moob Aug 19 '15 at 14:29
  • 1
    As long as the firstChild is the text to replace it will work. – epascarello Aug 19 '15 at 14:30
  • 1
    Right...if the markup changes, you'd be better off looping through and checking for the nodeType. – Jeff Aug 19 '15 at 14:31
2

Try to remove all text part from label, and then insert another text

$('label').contents().filter(function(){
    return this.nodeType === 3;
}).remove();

$('label input').before('Quick Search: ');

For reusable porpose, you can extend jQuery adding a method for that.

(function($) {

    $.fn.replaceText = function(oldText, newText) {
        this.contents().each(function(){
            if (this.nodeType === 3) {
                this.textContent = this.textContent.replace(oldText, newText);
            }
        });
        return this;
    };

}(jQuery));

$('label').replaceText('Search', 'Quick Search');
Guilherme
  • 1,980
  • 22
  • 23
0

I get the result but with dataTable search id :

$(document).ready(function(){
    $('#myTable').DataTable();
        
    $("#myTable_filter label").contents().each(function() {
        if(this.nodeType == 3){
            this.textContent = "Quick Search:";
            return false;
        }
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.datatables.net/1.10.8/js/jquery.dataTables.min.js"></script>
<link href="http://cdn.datatables.net/1.10.8/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table id="myTable">
  <thead>
    <tr><th>Col-1</th><th>Col-2</th></tr>
  </thead>
  <tbody>
    <tr><td>name 1</td><td>User-1</td></tr>
    <tr><td>name 2</td><td>User-2</td></tr>
    <tr><td>name 3</td><td>User-3</td></tr>
  </tbody>
</table>
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
0

Further to @jlbruno's answer here's a jQuery-free method that iterates over every label and case-insensitively replaces every instance of Search with Quick Search regardless of the position of the textNode.

(function(){
    
    var labels = document.querySelectorAll('label');
    for (l = 0; l < labels.length; ++l){
        var label = labels[l],
            nodes = label.childNodes;
        for (n = 0; n < nodes.length; ++n){
            var node = nodes[n];
            if(node.nodeType===3 && node.textContent.toLowerCase().indexOf("search")>=0){
             node.textContent = node.textContent.replace(/search/i,"Quick Search");
            }
        }
    }
    
})();
<label>Search:
  <input name="search" type="search"/>
</label>
<br />
<label>Do you like bees?
  <input name="beesPlease" type="checkbox"/>
</label>
<br />
<label>
    <textarea name="textarea">this instance of search will not be changed</textarea>
</label>
<br />
<label>Search This Mofo: 
  <input name="search-this" type="text"/>
</label>
<br />
<label>Fill me 
  <input name="search-that" type="text"/>
  with your search.
</label>
Community
  • 1
  • 1
Moob
  • 14,420
  • 1
  • 34
  • 47
-1

You could create a span tag inside the label and before the input. Give the span a id attribute and replace text inside that span with your text. It's better to prevent using html replacement for this.

Tom V.
  • 315
  • 2
  • 10
  • That is a good idea, however this HTML is generated by a JavaScript plugin (DataTables.net), therefore I do not have control over how that is generated. – dmcmulle Aug 19 '15 at 14:09
  • That's indeed something else. You could. But it's not the best way. Get the current html, replace parts of it and put it back. – Tom V. Aug 19 '15 at 14:11
-1

You'd need to put it in a separate element, you can't grab and update just the text and not the input with the way it's laid out currently.

The easiest way would be to use a span tag and then modify that like this:

<label id="my-label"><span>Search:</span>
  <input id="search-box" type="text"/>
</label>

and then use this to do modify it:

$('#my-label > span').text('Quicksearch:');

Fiddle: http://jsfiddle.net/orj8gkxc/

Tom Doodler
  • 1,471
  • 2
  • 15
  • 41
Styphon
  • 10,304
  • 9
  • 52
  • 86
  • @epascarello OK, I'll wait for your answer showing how then because with my knowledge of jQuery I don't know how to do it. – Styphon Aug 19 '15 at 14:12