jQuery - When input has text, do this

Hey!

I cant figure out how to solve this problem with meteors jQuerythingey, its not quite the same as regular jquery.

Template.search.events({
 'keypress .search': function (event, template){
   if( !template.$('.search').value ) {
	template.$('.results').hide();
  } else {
	template.$('.results').show();
   }	
  }
});

So basically if .search is empty. I want to hide .results
If .search has text inside it, i want to show .results

.search is a regular text input


I understand that “keypress” is wrong, but i have no idea what to replace it with. How do i create an active listener waiting for stuff to happen?

Like
>‘document’ : function (event){ do dis do dat }

Uhm, i dont know. Help me out dear meteor community!

What the problem is? You can’t hide/show? Or keypress isn’t triggered?

Keypress isnt triggered. However i dont think keypress is the best way to solve it. But it might work. Any ideas?

keydown or input events?

Keydown didnt work either. Im basically a beginner so i might do 50 different things wrong

I think you should use the “keyup” event. The “keydown” event triggers when the key is down and therefore the value of the key is submitted into the input only after the function is called.

but we can get it with event.currentTarget.innerText

Why is every one using jQ to get the values? e is already a jQ event/object so you can do just e.currentTarget.value - the .search is an input element right?

And the reason it is not working is simple :wink: jQ call does not return the string but the whole object within this making the call chainable, so you have to simply chain the call and do !template.$('.search').val(), but don’t do that, you can save some CPU time by utilizing e.

I dont know guys, this doesnt work :frowning:

view

<template name="search">
  <input id="search" type="text"/>
</template>

client

Template.search.events({
	'keyup #search': function (event, template){
		alert();
	}
});

I would do it on focus and pull the value from the search box. Have an if statement that checks what is in the value. If its blank then hide. else show search box

The answer is

Template.search.events({
	'keyup input#search': function (event, template){
		alert();
	}
});