Answer the question
In order to leave comments, you need to log in
How to show/hide bootstrap popover near input tag on button click?
Hello!
The page has an input element:
<input class="form-control" data-container="body" data-content="Enter at least three characters." data-placement="top" data-toggle="popover" id="id_nickname" name="nickname" placeholder="Enter nickname here" type="text" data-original-title="" title="">
<button id="test-popover" type="button" class="btn btn-default">Click!</button>
$("#test-popover").on('click', function(){
$('#id_nickname').popover('hide');
var anonymous_nickname = $("#id_nickname").val();
if (anonymous_nickname.length <= 2) {
$('#id_nickname').popover('show');
}
});
Answer the question
In order to leave comments, you need to log in
So far, the most acceptable option has given the following code:
$("#test-popover").on("click", function(){
var anonymous_nickname = $("#id_nickname").val();
if (anonymous_nickname.length <= 2) {
$('#id_nickname').popover('show');
} else {
$('#id_nickname').popover('destroy');
}
});
I could be wrong, but it's better to check for changes as you type:
$("#test-popover").on('keyup', function(){
var anonymous_nickname = $("#id_nickname").val();
if (anonymous_nickname.length <= 2) {
$('#id_nickname').popover('show');
}else{
$('#id_nickname').popover('hide');
}
});
It's best to attach an event to an input
$("#test-popover").bind('input', function(){
var anonymous_nickname = $("#id_nickname").val();
if (anonymous_nickname.length <= 2) {
$('#id_nickname').popover('show');
}else{
$('#id_nickname').popover('hide');
}
});
Both options didn't help. Again, it is necessary that the popover is displayed only when the user enters 0, 1 or 2 characters into the input with id="id_nickname" and presses the button with id="test-popover".
This piece of code should work for sure. You enter 1-2 characters in the text field, click on the button and what you need appears
$("#test-popover").on('click', function(){
var anonymous_nickname = $("#id_nickname").val();
if (anonymous_nickname.length <= 2) {
$('#id_nickname').popover('show');
}else{
$('#id_nickname').popover('hide');
}
});
$("#id_nickname").bind('input', function(){
var anonymous_nickname = $("#id_nickname").val();
if (anonymous_nickname.length <= 2) {
$('#id_nickname').popover('show');
}else{
$('#id_nickname').popover('hide');
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question