0

I have a searchbox as below:

<input type="text" class="form-control" onkeyup="myFunction()" id="myInput"/>

and I have a table with id="show_member" with different columns. I have written a code as below and I would like to search all columns whenever I type something in the searchbox(the input above) What is the problem with the code and how can I fix it? Not only doesn't it show the related columns but also it doesn't get removed when I delete all the letters in the search box.

    function myFunction(){
var input = document.getElementById("myInput");
//alert(input.value);
var iv = input.value;
var rows = $('table tr').hide().filter(":contains(".'iv'.")").show();
}

thanks for your help in advance

7
  • There are several examples out there for this: w3schools.com/howto/howto_js_filter_table.asp stackoverflow.com/questions/9127498/… And even a simple library if you're so inclined: listjs.com Commented Jun 20, 2017 at 13:39
  • you are making iv as string instead of a variable reference. Just remove the single quotes around it. Commented Jun 20, 2017 at 13:39
  • 1
    ":contains(".'iv'.")") this is php. Commented Jun 20, 2017 at 13:39
  • whenever I search, the <th> i mean the head part of my table is deleted. What should I do to disable it from deleting? Commented Jun 20, 2017 at 13:46
  • You can ignore the first row 'tr:not(:first)' Commented Jun 20, 2017 at 14:03

1 Answer 1

4

Use + for string operators(concatenation) in JavaScript

$('table tr').hide().filter(":contains(" + iv +")").show();

instead of

$('table tr').hide().filter(":contains(".'iv'.")").show()
Sign up to request clarification or add additional context in comments.

Comments

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.