Message on a label p

1

I want to show a message on a label

in a login that if the user has not entered any data that shows a message that you have not entered anything in that tag this is the code that I am using:

(function () {

    "use strict";

    var iniciar = document.getElementById('iniciarSesion');
    iniciar.addEventListener('click', function () {
        alert();
        var user    = document.getElementById('txtUsuario').value,
            pass    = document.getElementById('txtPass').value,
            msg     = "";


        if (user !== "" && pass !== "") {

        } else {
            msg = "alaal";
            document.getElementById('mensaje').value = msg;
        }

    });
}());

I know I need AJAX and that, but first I want you to show me the message just to get started.

This is the login code

I'm just starting in JavaScript

    
asked by DagsDroid 29.07.2017 в 21:25
source

5 answers

0

There are several ways to do what you are looking for, but the basic concept is the manipulation of the DOM with JavaScript.

If the structure of your page is static, has a single p-tag or the one of your interest is the first you can use document.querySelector('p').innerHTML or document.getElementsByTagName('p')[0] .

If the situation were different, there is a wide variety of alternatives.

Example

document.querySelector('p').innerHTML = '¡Hola mundo!';
<p></p>
    
answered by 30.07.2017 / 16:22
source
1

You can do something like that, as @Aaron mentioned:

	(function (){
		"use strict";
		const parrafo_de_error = document.getElementsByTagName("p")[0];
		document.getElementsByTagName("button")[0].addEventListener("click", function(){
			const usuario = document.getElementById("txtUsuario").value,
				pass = document.getElementById("txtPass").value;
			!usuario && !pass ? parrafo_de_error.textContent = "Escribe el usuario y contraseña" : parrafo_de_error.textContent = null;
		});
	})();
<input type="text" id="txtUsuario">
<input type="password" id="txtPass">
<button>Iniciar Sesión</button>
<p style="color: red"></p>

Occupy the ternary operator to make it shorter (condición) ? (true) : (false);

    
answered by 29.07.2017 в 22:00
1

I think with what you change:

document.getElementById('mensaje').value = msg;

By:

document.getElementById('mensaje').innerHTML = msg;

It would be enough for you to work;)

    
answered by 30.07.2017 в 14:52
1

In the html you can add the tag p, with an id to call it. You put it where you want it to appear.

<p id="mensaje"></p>

To show a message from the script, you can upload it to its text property.

document.getElementById("mensaje").text = "Mensaje en pantalla";

And so that "the message disappears" you can load it with a vacuum.

document.getElementById("mensaje").text = "";
    
answered by 29.07.2017 в 21:36
1

You can do something like that, like @Aaron mentioned, and like the comment above, I'm just doing some modifications since you said you were starting in javascript

var d = document,
  parrafo_de_error = d.getElementById("error");
d.getElementById("enviar").addEventListener("click", function() {
  var usuario = d.getElementById("txtUsuario").value,
    pass = d.getElementById("txtPass").value;
  usuario !== "nombre_de_usuario" || usuario.length == 0 || pass !== "prueba" || pass.length == 0 ? parrafo_de_error.textContent = "Nombre de usuario y contraseña incorrecta" : parrafo_de_error.textContent = null;
});
<input type="text" id="txtUsuario">
<input type="password" id="txtPass">
<button type="submit" id="enviar">Iniciar Sesión</button>
<p id="error" style="color: red"></p>

Occupy the ternary operator to make it shorter (condición) ? (true) : (false);

The Ternary Operator

is a simplification of the conditionals if, else, only that it is simpler and in turn evaluates conditions only evaluates the conditions of flash and true, which are totally required in the if, else shortland

    
answered by 30.07.2017 в 04:17