Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - how to show DIV after putting text into <input>

I'm trying to write function in JavaScript which will show/hide 'DIV' after putting some text into .

I managed to write it, however I cannot to make it work only if user put to the 'input' value bigger than 8.

Html:

<input type='text' id='area' style='border: 1px solid;'></input><br>
<div id='text1' style='display:none; '>Examletext</div>

JavaScript:

$(document).ready(function() {
    $("#area").keyup(function() {
        if ($('#text1').is(":hidden")) {
            $('#text1').show(500);
        } else {
            $("#text1").hide(500);
        }
    });
});

Above is working script, but this works whatever you put into the 'input'. I want execute the script only when I put value bigger than 8 (9, 10, 101 etc.)

I tried to add this(no effect):

if ($("#area").value > 8){}

Here is the working script where I have commented out line described above - jsfiddle

like image 309
user2463248 Avatar asked Jan 22 '14 14:01

user2463248


3 Answers

change $("#area").value to $("#area").val()

$(document).ready(function() {
    $("#area").keyup(function() {
        if ($("#area").val() > 8) {
            if ($('#text1').is(":hidden")) {
                $('#text1').show(500);]
            } else {
                $("#text1").hide(500);
            }
        }
    });
});

In jQuery you need to use val() not value

Fiddle Demo

Documentation : http://api.jquery.com/val/

like image 161
Pranav C Balan Avatar answered Oct 17 '22 07:10

Pranav C Balan


It's not .value but val()

Your jsfiddle should be

if ($("#area").val().length > 0){
like image 24
juanignaciosl Avatar answered Oct 17 '22 07:10

juanignaciosl


Try this:

$(document).ready(function() {
$("#area").keyup(function() {
    if ($("#area").val() > 8){
        if ($('#text1').is(":hidden")) {
            $('#text1').show(500);
        } else {
            $("#text1").hide(500);
        }
    }
 });
});
like image 34
user3206210 Avatar answered Oct 17 '22 06:10

user3206210