0

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.

3 Answers 3

2

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");
});
Sign up to request clarification or add additional context in comments.

1 Comment

haha No problem. Btw, insert '&& $vari != ""' in the if statement to prevent the display to change with any keyup with an empty input field ;)
1
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>

Comments

1

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/

3 Comments

Sorry, this doesn't work. Test out the script: using the code your provided, my script still does not work.
@user40531 - It was missing the closing parentheses. Updated, with a fiddle.
You got my upvote! :) Also: by any chance, do you know how to make "contain" NOT case-sensitive?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.