3

I wrote a few lines of jQuery code, but don't know how to realize this in plain JavaScript. That output:

echo "<tr id='perf_row' class='table_header_tr' name='tr_$row_fio_perf[0]'>
 <td class='table_header_td' name='td_$i'>
 $i
 </td>
 <tr>";

search input. When user inputs chars, table rows (output higher) must hide and show only what matches the search word.

<input type="text" onkeyup="showPerformers();" id="ShowSearchPerformers">
 function showPerformers(){ 
 var search_login = $('#ShowSearchPerformers').val();
 search_login = search_login.toLowerCase(); 
 $('tr[name^=tr_]').hide();
 var search = 'tr[name*=' + search_login+']';
 $(search).show(); 
 }

How to write this function in JavaScript, not jQuery?

ЯegDwight
25.4k10 gold badges47 silver badges52 bronze badges
asked Oct 9, 2012 at 8:44
5
  • Why don't you want to use jQuery? Commented Oct 9, 2012 at 9:02
  • 1
    What's your browser target? Being able to use querySelectorAll would be of great help here, yet it won't work in IE7. Commented Oct 9, 2012 at 9:03
  • felix, i learn before jquery, now try to learn js)) Commented Oct 9, 2012 at 9:05
  • m90, last version chrome and firefox, i can hide all table rows but dont know how to show them, can you give me the scheme, only functions which can help me to do this and i'll do it) Commented Oct 9, 2012 at 9:07
  • 1
    It boggles me how this question, which relates to core javascript language, is flagged as too localized. Commented Jan 21, 2015 at 7:52

1 Answer 1

3

I would suggest a different approach all together, but based on your code, and not over-complicating things too much:

<input type="text" onkeyup="showPerformers(this,event);" id="ShowSearchPerformers">

Passing the input element to the function saves a DOM scan (getElementById, or in jQuery $('#id')). Also passing the event object might prove useful given time.

function showPerformers(search_login,e)
{
 search_login = search_login.value.toLowerCase();
 var table = document.getElemebtById('YourTblId');
 var row = table.getElementsByTagName('tr');
 var nameSearch = new RegExp('^tr_' + search_login);
 for (var i=0;i<row.length;i++)
 {
 if (row[i].name.substr(0,3) === 'tr_')
 {
 row[i].style.display = '';//make previously hidden rows visible again
 if(!row[i].name.match(nameSearch))
 {//row name starts with tr_, but not tr_[searched number]
 row[i].style.display = 'none';
 }
 }
 }
}

If I'm not mistaken, this should work, and perform as you expect it to. It does need some fine-tuning though, but I wouldn't want to spoil your fun :). Just a tip, as it now stands, you have no guarantee that the user-input is numeric: entering a would hide all rows, and show none, for example. You could fix that by either:

value.replace(/[^0-9]/g,'');//leaves only numbers
Math.floor(parseFloat(value));//Math.floor(parseFloat(' 123.1s')) --> 123

Just play a little, BTW this site is a great sandbox for stuff like this

answered Oct 9, 2012 at 9:05
Sign up to request clarification or add additional context in comments.

2 Comments

i tried this code, not work, but i'll fix that strictly to my example. Truth Is Out There! thanks
@user1506183: try setting up a fiddle with the code you have (one for the working code, another for the "pure JS" code, maybe somebody here can see the problem. It's hard to write correct code for a DOM tree I can't see...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.