3

I'm using datatables jquery plugin for beautifying my tables. I'm trying to stylize the search box to look more like this

CSS3 search box

However the javascript generated code for the current search box in datatables looks something like this

<div class="dataTables_filter" id="countstable_filter">
    <label>Search: 
          <input type="text" aria-controls="countstable" placeholder="Search">
    </label>
 </div>

I was able to get javascript to add a placeholder attribute to the search box. But I can't figure out how to remove the Search: text. I have seen a few solutions on google, but they required the label to have an id which I don't have here.

4 Answers 4

6

Since you're using DataTables you could also turn off the Search: string by changing the language variable sSearch see more documentation on oLanguage.sSearch and jsBin.

$("#someTable").dataTable({
  oLanguage: {
    sSearch: ""
  }
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can remove the text node containing Search: using:

$("#countstable_filter label").contents().first().remove();

.first() is because it is the first node (a text node) of the label. Functions such as .contents() and .first() enable you to find nodes (traverse the DOM) without needing an ID. Essentially, you start with an element and walk your way through the DOM with specific functions until you've reached the element wanted.

1 Comment

Deleted my incomplete answer because to complete it would, essentially, duplicate your own. +1!
1
var x=document.getElementsByTagName("input");
for(var i=0;i<x.length;i++){
    var obj = x[i];
    if(obj.getAttribute("type")=='text' && obj.getAttribute("aria-controls")=='countstable' && obj.getAttribute("placeholder")=='Search'){
    // do stuff with the object
}
}

Comments

1
var label = $('label', '.dataTables_filter');
    label.html(label.children());

FIDDLE

children() will not include the text node, so just rewrite the content with the elements only, and get rid of the text node.

4 Comments

I am still learning javascript. How does $('label', '.dataTables_filter'); work ?
It uses context, basically it's just a shortcut for $( '.dataTables_filter').find('label'), which finds any label element withing the element with class dataTables_filter. You can use any selector you like, and $('#countstable_filter label') is simpler and better IMO, I just did'nt want to copy code, so came up with something else. The selector is'nt really the answer, the method is, and I my opinion this was simpler than contents().first() ??
I can't really say if it's simpler. I'm new to JS and the other way seems a bit simpler on looking, maybe because I'm used to scraping DOM with Nokogiri and normally work with that methodflow.
@devcoder - Thats completely up 2 u, both ways should work, and pimvdb's way is a good way of doing it, and probably more the way it was meant to be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.