Different concatenation ?

Asked

Viewed 186 times

4

Guys I’m starting programming has little time and I have to register only I want the information typed by the user as the name, email and age appear through an "Alert" after he click on the button "Accept".

My HTML and Javascript files are already "interconnected", I do not know if it is possible to do so, if it is not present me the best way to do this using only HTML and Javascript, and if it is 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")   
 }

On hold
Att,
Lone

1 answer

5


Concatenation is simple, just use the +, which is contextual (numerical sum and concatenate strings).

To make life easier, it’s interesting to use a 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 the 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)
}

Have other ways to access elements without id, but the id facilitates reading and simplifies access. We could, for example, have used document.getElementsByName, but a list of nodes is returned instead of a single element, as the names can be repeated in the same document.

See working on CODEPEN.

  • I remember that it is also possible to use this "getElementById" next to the onClick function with images, because I will also need the user to click on the image the program call another and if he clicks back to the previous image or will be two images. What would be the right way to do this ? And thank you for the previous answer.

  • 1

    getElementById serves to reference any element of the DOM, which you will do with the element after only depends on the context. As for what you asked, it depends a lot. It would be the case to open a new question and try to explain in as much detail as possible. But take a prior search for the site, because already have some questions answered about image galleries.

  • I took a look at galleries but it doesn’t have exactly what I want, what I wanted was to make an effect that when clicking on the image it changed color calling another and if I clicked again it would change to the color that was at the beginning or be the same image. But I’m not in need of it urgently, another day I’ll do another topic however you managing to help me here would already help.

  • 1

    @Lonetonberry it seems to me that what you asked has been answered, if you have any new questions open a new question.

Browser other questions tagged

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