How do I open a floating div tooltip style by clicking an input?

Asked

Viewed 2,288 times

3

Hello.

I got a problem I can’t fix. I have a text input and I needed when I clicked on it to open a window or floating div underneath it like a tooltip but with HTML inside it.

How can I make that when I click on a <input> display a floating div as tooltip underneath the <input>? Thus:

inserir a descrição da imagem aqui

I need to make it look like this image. I tried it with tooltip but it didn’t work because it disappears when I get the mouse on top of it. I thought I’d use a div.

<div class="info-input-style">
    <input type="text" name="grupos" id="grupos">
    <div id="conteudo">
        <p>HTML DENTRO DA DIV #CONTEUDO </p>
    </div>
<div>

But I have no idea how to do that. Can anyone help me?

  • @Leocaracciolo and a normal input. I just need to do that when clicking on the input open that toltip with a input in. and not a modal

  • @Leocaracciolo Yes. but unfortunately I cannot use modal

  • See if you want something like this: http://jsfiddle.net/BumU5/1138/

  • have an example up to W3... https://www.w3schools.com/howto/howto_css_tooltip.asp

  • if you have already styled, so that it appears in the correct location and such, follow this link, https://answall.com/questions/191776/detectar-evento-click-fora-do-element :) so that it closes when you click on it, or off it

3 answers

3


You can make the <div> floating when you click on <input> in many ways.

I present one using Jquery.

Note: I chose to format a bit the HTML and CSS to be similar to the one presented in the image, and for that reason it was still a little extensive.

   
$("#grupos").click(function(){
  $("#conteudo").show(); //quando clica no input mostra o conteudo
});

$("body, #aplicar").click(function(){
  $("#conteudo").hide(); //quando aplica ou clica fora esconde
});

//este é preciso também para não esconder quando clica no grupos ou conteudo
$("#grupos, #conteudo").click(function(e){
  e.stopPropagation(); 
});
#conteudo {
  margin-top:17px;
  padding:0px 20px;
  box-shadow:0px 0px 3px #777;
  width:250px;
  color:gray;
  display:none;
  position:relative;
}

#conteudo h1 {
  font-size:20px;
  margin:0;
}

#conteudo input {
  width:180px;
}

#conteudo:before { /*a seta do div flutuante*/
  content: "";
  position: absolute;
  top: -10px;
  left: 135px;
  height: 20px;
  width: 20px;
  background-color: white;
  transform: rotate(45deg);
  box-shadow: -2px -2px 1px #ccc;
}

#aplicar {
  text-decoration:none;
  color:#2dabb9;
  font-size:15px;
  position:absolute;
  right:5px;
  bottom:5px;
}

.bloco {
  padding:20px 0px;
  position:relative;
}

.bloco:not(:last-child) {
  border-bottom:1px solid #ccc;
}

label {
  display:block;
  color:#5f5f5f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="info-input-style">
    <label>Label</label>
    <input type="text" name="grupos" id="grupos">
    <div id="conteudo">
        <div class="bloco">
          <h1>Titulo 1</h1>
        </div>
        <div class="bloco">
          <input type="text" placeholder="Insira a sua cidade de destino">
        </div>
        <div class="bloco">
          <a href="#" id="aplicar">Aplicar</a>
        </div>
    </div>
<div>

0

Keep swelling our plugins and libraries document will only detonate traffic

A simple Javascript solves the problem. CSS is on its own!

document.onclick=evento; 
function evento(e){ 
var target = (e && e.target) || (event && event.srcElement); 
var obj = document.getElementById('minhadiv'); 
var obj2 = document.getElementById('clica'); 
checkParent(target)?obj.style.display='none':null; 
target==obj2?obj.style.display='block':null; 
} 

function checkParent(t){ 
while(t.parentNode){ 
if(t==document.getElementById('minhadiv')){ 
return false 
} 
t=t.parentNode 
} 
return true 
}
<input id="clica" type="text" value="2">

<div style="display: none;" id="minhadiv">
	
	<h3>Titulo 1</h3>

	<input type="text" name="cidade" placeholder="Insira sua cidade de destino"> 
	<a href="#" id="aplicar">Aplicar</a>
	
</div>

If you’re not going to make a great framework, or complex system, or giant website, consider using pure Javascript.

  • In your reply when I click on the input that is inside the <div style="display: none;" id="showDiv" onClick="hide('showDiv')"> she goes away

  • @user is, sometimes we forget to do some tests and give in what gave, but the solution is to remove the function onClick="hide('showDiv')" DIV AND TAG <p class="fechar" onClick="hide('showDiv')"> Answer already edited!

0

In the script, you can switch to other events like Hover, etc...

Css:

.popover {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1060;
  display: none;
  max-width: 276px;
  padding: 1px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  font-style: normal;
  font-weight: normal;
  line-height: 1.42857143;
  text-align: left;
  text-align: start;
  text-decoration: none;
  text-shadow: none;
  text-transform: none;
  letter-spacing: normal;
  word-break: normal;
  word-spacing: normal;
  word-wrap: normal;
  white-space: normal;
  background-color: #fff;
  -webkit-background-clip: padding-box;
          background-clip: padding-box;
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, .2);
  border-radius: 6px;
  -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
          box-shadow: 0 5px 10px rgba(0, 0, 0, .2);

  line-break: auto;
}

.popover.bottom {
  margin-top: 10px;
}

.popover-title {
  padding: 8px 14px;
  margin: 0;
  font-size: 14px;
  background-color: #f7f7f7;
  border-bottom: 1px solid #ebebeb;
  border-radius: 5px 5px 0 0;
}
.popover-content {
  padding: 9px 14px;
}
.popover > .arrow,
.popover > .arrow:after {
  position: absolute;
  display: block;
  width: 0;
  height: 0;
  border-color: transparent;
  border-style: solid;
}
.popover > .arrow {
  border-width: 11px;
}
.popover > .arrow:after {
  content: "";
  border-width: 10px;
}
.popover.top > .arrow {
  bottom: -11px;
  left: 50%;
  margin-left: -11px;
  border-top-color: #999;
  border-top-color: rgba(0, 0, 0, .25);
  border-bottom-width: 0;
}
.popover.top > .arrow:after {
  bottom: 1px;
  margin-left: -10px;
  content: " ";
  border-top-color: #fff;
  border-bottom-width: 0;
}

.popover.bottom > .arrow {
  top: -11px;
  left: 50%;
  margin-left: -11px;
  border-top-width: 0;
  border-bottom-color: #999;
  border-bottom-color: rgba(0, 0, 0, .25);
}
.popover.bottom > .arrow:after {
  top: 1px;
  margin-left: -10px;
  content: " ";
  border-top-width: 0;
  border-bottom-color: #fff;
}

HTML

   <div class="bgform-2 popover fade bottom in">
     <div class="arrow"></div>
     <h3 class="popover-title">Titulo 1</h3>
    <div class="popover-content">
      <form>
         <fieldset>
         <legend>Form</legend>
         <label>Nome: </label>
             <input type="text" placeholder="digite seu nome completo"/>
             <input type="submit" value="Enviar" /> 
         </fieldset>
      </form>
    </div>
 </div>
<button type="submit" id="btn">
                            Enviar
</button>

Jquery

$("#btn").click(function(){
        $('.popover').toggle(250);
    })

Following demonstration...

https://jsfiddle.net/8k4akkL3/2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.