Reputation: 2704
I have an global search input
and when the user start typing it should display a hidden div
with those results.
I cant get the div to display when keyup. Here is my fiddle
Thats my div:
<div class=" container-fluid parent">
<input class="form-control" placeholder="Search ..." id="GlobalSearchInput" style="padding:10px;">
<div class="" id="showSearchDiv" style="display:none;margin-top:10px;height:450px;background-color:red;"></div>
</div
$(document).ready(function() {
$("#GlobalSearchInput").keyup(function() {
function showGlobalSearch() {
var x = document.getElementById('showSearchDiv');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
});
});
Upvotes: 0
Views: 8077
Reputation: 5690
check this code and your code have some problem 1. need to use close tag and main this no need to use function for check this cause you already check onkey function
$(document).ready(function() {
$("#GlobalSearchInput").keyup(function() {
var input_value = document.getElementById('showSearchDiv');
// when value is not empty then div will show and
//when input value empty then div will hide
if($(this).val() == "") {
input_value.style.display = 'none';
} else {
input_value.style.display = 'block';
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=" container-fluid parent">
<input class="form-control" placeholder="Search ..." id="GlobalSearchInput" style="padding:10px;">
<div class="" id="showSearchDiv" style="display:none;margin-top:10px;height:450px;background-color:red;"></div>
</div>
Upvotes: 1
Reputation: 18987
I have an global search input and when the user start typing it should display a hidden div with those results
So from what I can understand you need to display the div when ever user is typing something and hide it when nothing is typed. Try the below approach.
Below is the sample code
$(document).ready(function() {
$("#GlobalSearchInput").keyup(function() {
if ($.trim($(this).val()).length) {
$('#showSearchDiv').show();
} else {
$('#showSearchDiv').hide();
}
});
});
Upvotes: 1
Reputation: 4919
This is because in your keyup
event handler you define a showGlobalSearch
which is never called.
Also defining a function is not useful here, unless you need the same code anywhere in your code.
So, here is the corrected code.
$(document).ready(function() {
$("#GlobalSearchInput").keyup(function() {
var x = document.getElementById('showSearchDiv');
if($(this).val() == "") {
x.style.display = 'none';
} else {
x.style.display = 'block';
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=" container-fluid parent">
<input class="form-control" placeholder="Search ..." id="GlobalSearchInput" style="padding:10px;">
<div class="" id="showSearchDiv" style="display:none;margin-top:10px;height:450px;background-color:red;"></div>
</div>
Upvotes: 2
Reputation: 24147
Two problems:
function showGlobalSearch() {
(plus the corresponding }
.)Also, I'd recommend to use jQuery as much as possible, and not mix it with other means to access the DOM (so no getElementById
).
The result:
$(document).ready(function() {
$("#GlobalSearchInput").keyup(function() {
var div = $('#showSearchDiv');
if ($("#GlobalSearchInput").val().length > 0) {
div.show();
} else {
div.hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid parent">
<input class="form-control" placeholder="Search ..." id="GlobalSearchInput" style="padding:10px;">
<div class="" id="showSearchDiv" style="display:none;margin-top:10px;height:450px;background-color:red;"></div>
</div>
Upvotes: 1