user2031330
user2031330

Reputation:

jQuery script is not functioning

When I try to run this jQuery script:

$('#textbox').keyup(function() {
$vari = $(this).val();
$(".user:contains($vari)").css("display", "block");
};

it doesn't function, it just appears that the script doesn't really do anything!

This is the accompanying HTML:

<div class="user">hello</div>
<input type="text" id="textbox">

Please advice about what I have to do to get this script functional. It should grab the div with the class user, and if the text in the textbox with the id textbox has a value which is also in the user, it should make it visible.

Upvotes: 0

Views: 55

Answers (3)

Alexandre Wiechers Vaz
Alexandre Wiechers Vaz

Reputation: 1617

To make it not case-sensitive as you want, here's an variation to @Joseph fiddle

$('#textbox').keyup(function() {
var $vari = $(this).val();
var reg = new RegExp($vari, "gi");
var $div = $(".user").html();
if($div.match(reg))
    $(".user").css("display", "block");
});

Upvotes: 2

Joseph Silber
Joseph Silber

Reputation: 219920

You have to concatenate your $vari variable into the selector:

$('#textbox').keyup(function() {
    var $vari = $(this).val();
    $(".user:contains(" + $vari + ")").css("display", "block");
});

Here's the fiddle: http://jsfiddle.net/rg8nU/

Upvotes: 1

Sagar Hirapara
Sagar Hirapara

Reputation: 1697

do this 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {

      $('#textbox').keyup(function() {
    var $vari = $(this).val();
    $(".user:contains(" + $vari + ")").css("display", "block");
});
    });
</script>

 <div>
        <input type="text" id="textbox" />

   </div>
   <div  class="user" style="display:none">a</div>

Upvotes: 1

Related Questions