简体   繁体   中英

How can show a div when input has a value?

I have an input text that and a hide div.

I want to show the div when the input text has a value.

Here is the demo: http://jsfiddle.net/vyc7N/181/

<label for="db">Type whatever</label>
<input type="text"name="amount"  />

<div id="yeah" style="display:none;">
 <input type="submit"   />
</div>

Please somebody can help me?

You can write the code in keyup event of textbox to get the length of text entered in it.

$('input[name=amount]').keyup(function(){
  if($(this).val().length)
    $('#yeah').show();
  else
    $('#yeah').hide();
});

Working Demo

you can also use .toggle() instead of using .show / .hide() :

  $('input[name=amount]').keyup(function(){
     $('#yeah').toggle($(this).val().length);
  });
$('input[name=amount]').bind('keyup change', function(){
if(!$(this).val())
    $("#yeah").css('display', 'none');
else
    $("#yeah").css('display', '');
});

This is pure javascript and DOM, no jQuery

You could use onkeyup() :

<label>Type whatever</label>
<input id='inptOne' type="text" name="amount" onkeyup="myFunction()" />

<div id="yeah" style="display:none">
    <input type="submit" />
</div>

<script>
//if input has some value will show the button
  window.myFunction = function() {
  //alert('it ran!');
  var hasValue = document.getElementById('inptOne').value;
  if (!!hasValue) {
    document.getElementById('yeah').style.display = 'inline';
  } else {
    document.getElementById('yeah').style.display = 'none';
  };
 };
</script>

Click Here for jsfiddle

Working fiddle

Use following logic : If after trimming there is some value , then show else hide.

$('input[name=amount]').keyup(function(){
  if($.trim($(this).val())!='')
    $('#yeah').show();
  else
    $('#yeah').hide();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM