Concatenation different?

4

I'm starting to have a short time and I have to register only because I want the information entered by the user such as name, email and age to appear through an "alert" after he clicks the " Accept ".

My HTML and Javascript files are already "interconnected", I do not know if it's possible to do this, if not I'll show you the best way to do it using only HTML and Javascript, and if so please show me how.

  

My HTML file       

    <label for="nome">Nome:</label>
    <input type="text" name="nome" size="20" maxlength="20" placeholder="Seu nome"/><br><br>
    <label for="senha">Senha:</label>
    <input type="password" name="senha" size="20" maxlength="25" placeholder="Sua senha"/><br><br>
    <input type="button" value="Aceitar" onClick="aceitar()">
    <a class="none" href=javascript:history.back();><input type="button" value="Cancelar"></input></a>

</form>
  

My JS file

function aceitar(){
    alert("Aqui seria a concatenação")   
 }

I look forward to Att, Lone

    
asked by anonymous 14.01.2017 / 23:30

1 answer

5

The concatenation is simple, just use + , which is contextual (sum numeric and concatenates strings ).

To make life easier, it's interesting to use id in the elements:

<label for="nome">Nome:</label>
<input type="text" id="nome" name="nome" maxlength="20" placeholder="Seu nome"/><br><br>
<label for="senha">Senha:</label>
<input type="password" id="senha" name="senha" maxlength="25" placeholder="senha"/><br><br>
<input type="button" value="Aceitar" onClick="aceitar()">

So, you can use document.getElementById to access the fields, and consequently, their value with .value

function aceitar() {
    var nome = document.getElementById('nome');   // Aqui pegamos o campo, e não seu valor
    var senha = document.getElementById('senha');
    // em seguida usaremos o .value para pegar o valor dos campos
    alert('Seu nome é ' + nome.value + 'e sua senha é ' + senha.value)

    // no caso especifico, poderiamos ter feito assim tambem:
    // var nome= document.getElementById('nome').value;
    // var senha = document.getElementById('senha').value;
    // alert('Seu nome é ' + nome + 'e sua senha é ' + senha)
}

It has other ways of accessing elements without id , but id makes reading easier and simplifies access. We could for example have used document.getElementsByName , but a list of nodes is returned instead of a single element, since the names can be repeated in the same document.

See working at CODEPEN .

    
14.01.2017 / 23:41