How to switch txt message according to the field’s Focus

Asked

Viewed 192 times

0

In that question I saw how to read the .txt ~> Read txt

Now I want to know: In case I leave a field on phocus, and is about the message of tooltip, it must show a message concerning that field.

Ex: In case I leave the password field with phocus and go over the "?" of tooltip it must show a message concerning the password field. And should you leave the name field with phocus, show a message concerning it.

<style> .tooltip {
  display: inline;
  position: relative;
}
.tooltip:hover:after {
  padding: 5px 15px;
  width: 220px;
  border-radius: 5px;
  background: #333;
  background: rgba(0, 0, 0, .8);
  content: attr(data-title);
  position: absolute;
  left: 20%;
  bottom: 26px;
  z-index: 98;
  color: #fff;
}
.tooltip:hover:before {
  border: solid;
  border-color: #333 transparent;
  border-width: 6px 6px 0px 6px;
  content: "";
  position: absolute;
  left: 50%;
  bottom: 20px;
  z-index: 99;
}
</style>
<tr>
  <td align="right">
    <font face="arial" color="blue" size="-1">Senha do Usuário :</font>
  </td>
  <td>
    <input type="text" align="left" name="tx_senh_usua" size="7" value="SEDS" readonly="true">
  <td align="right">
    <font face="arial" color="blue" size="-1">Nome :</font>
  </td>
  <td>
    <input type="text" align="left" name="tx_nome_usua" size="7" value="" readonly="true">   
    </td>
    <td>
    <span style="color: blue;" data-title="Senha padrão para novos usuários." class="tooltip">?</span>
  </td>
</tr>

Simplifying ... I want to create a file containing messages for various fields. And if the field is in phocus and the "?" of tooltip, he should check in that file which message to the given field!

It is possible to do this?

  • 1

    Sorry, I ended up editing fast and put PHP together!

  • Can you leave any field with phocus if they are readonly?

  • The field with readonly was just an example of the file with a message!

  • To print the text . txt in tooltip you know, now just create a tooltip for it. instead of .tooltip:hover, use .tooltip:focus.

  • But then how will I know what message you have to show for that Phocus?

1 answer

1


To do this you need Javascript to change the content of Tooltip.

A suggestion is to also use a field data- inputs that have Focus and fetch this string inside the tooltip. In the example below I also changed the classes a little to be able to remove the tooltip if there is no phocus.

Example:

<input type="text" data-title="Senha padrão para novos usuários." align="left" name="tx_senh_usua" size="7" value="SEDS">

Javascript:

document.querySelector('.tltp').onmouseover = function () {
    if (document.body == document.activeElement) return this.classList.remove('tooltip');
    var texto = document.activeElement.getAttribute("data-title");
    if (texto) {
        this.setAttribute("data-title", document.activeElement.getAttribute("data-title"));
        this.classList.add('tooltip');
    }
}

jsFiddle: http://jsfiddle.net/q34zd30s/

I could create an array in JS with these tooltips and compile directly from PHP to JS.


Following the idea I presented in your other question will receive from PHP something like:

'{"tx_senh_usua":"Senha do Usu\u00e1rio :","tx_nome_usua":"Nome :"}'

So you can compile on the client side:

var json = JSON.parse(<a string do servidor>);

To fetch this info in tooltip you can do so:

var texto = json[document.activeElement.name]; 
this.setAttribute("data-title", texto);

Example: http://jsfiddle.net/9s3ck057/

  • Then @Sergio, it will show you if the field is in Focus. But it has how to make that instead of taking the data-title of the input itself, take from a file . txt? **Following the answer given by Antony Alkmim in the other question!

  • @Alexandre indirectly yes. Either send the request by ajax and do it in PHP and return it to AJAX, or echo it in PHP to a Javascript array (which compiles with the page) and use it from there. Which prefer?

  • It would be easier for my PHP echo problem for a JS array!

  • @Alexandre and know how to do it? or want to add something more to the answer?

  • I’m sorry @Sergio, but I have no idea how to do e-e' If you can give a light I thank you!

  • @Alexandre can put an example of how the file . txt is? could put a JSON there?

  • in the file I put the field setting the text that should appear (because I thought it would show the message). Nome ~> Informe o nome de usuário &#xA;RG ~> Informe o RG do usuário&#xA;Telefone ~> Informe o Telefone do usuário&#xA;Senha ~> Senha padrão para novos usuários

  • @Alexandre ok, and separated by lines? and this key, what is the relationship with HTML? how many keys/fields have?

  • are separated by lines, how much this key I put, was only to set from which field it comes. And currently are only these fields!

  • @Alexandre edited the answer and added the idea of the other answer. Tell if you can make it work.

  • I implemented what you went through! But it didn’t work out ... Thanks anyway @Sergio

  • @Alexander that part did not work?

  • That stretch var json = JSON.parse($texto); that comes from <?php json_encode($texto);?>

  • Alexandre: jump here: http://chat.stackexchange.com/rooms/19388/discussion-between-sergio-and-alexandre

  • @Alexandre jumps into the chat to help you: http://chat.stackexchange.com/rooms/19433/sala-meeting

Show 10 more comments

Browser other questions tagged

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