Change display property of a <div> by clicking on an <input>

Asked

Viewed 1,360 times

-3

I’m working on a contact form that when the person clicks on <input> there appears a <div> by the side, mentioning some things the person should do. Ex: Enter your email here.

How to make the property aggregate display:block in <div>'s:

<div class="tooltip.right">

By clicking on <input>s:

<input class="form-control">

Note: When the person clicks outside the <input> alters to display:none. Pure Javascript.

1 answer

6


Your question has become too vague, I’m not sure if the input points to a specific div, whether you will have multiple inputs and multiple Ivs.

Yet the answer would be something like:

You can use the property .style or . classname`:

  1. style:

        window.onload = function() {
             var div = document.querySelector("div[class='tooltip.right']");
             var input = document.querySelector(".form-control");
    
             input.onclick = function() {
                  div.style.display = "block";
             };
    
             input.onblur = function() {
                  div.style.display = "none";
             };
        };
        div[class="tooltip.right"] {
              display: none;
        }
        <input class="form-control">
        <div class="tooltip.right">Olá mundo</div>

  2. classname:

        window.onload = function() {
             var hideClass = /(^|\s)hide(\s|$)/;
    
             var div = document.querySelector("div[class*='tooltip.right']");
             var input = document.querySelector(".form-control");
    
             input.onclick = function() {
                  div.className = div.className.replace(hideClass, " ");
             };
    
             input.onblur = function() {
                  div.className += " hide";
             };
        };
        .hide {
              display: none;
        }
        <input class="form-control">
        <div class="tooltip.right hide">Olá mundo</div>

Note:

I recommend not using point classes class="tooltip.right", the point is a delimiter to identify the classes in the CSS selectors, it would be better something like:

<div class="tooltip.right hide">Olá mundo</div>

Or if you’re using the point as separator and tooltip and right are two different classes, then the correct one to separate in HTML is space, like this:

<div class="tooltip right hide">Olá mundo</div>
  • I updated the question

  • I think it’s not quite what I’m looking for. It’s something very simple. Too much code for too little.

  • I just want the <div> that has the class="tooltip.right" appears when the person clicks on a <input> with class="form-control".

  • The way you did, the <input> disappears when clicked. and the <div> emerges.

  • Yes, there are several. All with the same classes.

  • Both the <div>'s as the <input>'s

  • 1

    @Alexandrelopes ae complica se tem várias Divs, I don’t even know how is the whole HTML structure, I recommend reading this topic: http://answall.com/help/mcve and trying to improve the question, understand as a constructive criticism.

  • Gui, it is possible to do without inserting a class=""? Only style="display:block"? :)

  • 1

    @Alexandrelopes the first example is exactly that, without class. See I used div.style.display = "block"; and div.style.display = "none";

  • I’ll test it here! D

  • 2

    @Alexandrelopes if you can’t send me the HTML structure of the form, and I make the modifications to work in your form.

  • I’ll send you here!

  • Look at the fiddle: https://jsfiddle.net/sLLdqmqx/

  • I’ll give you the right answer. : D

  • @Alexandrelopes this is just a div and an input, I asked for the form, need know the structure DOM modify the code. I am waiting.

Show 10 more comments

Browser other questions tagged

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