Change and disable field after selection

Asked

Viewed 8,060 times

1

I believe I already have a question answered more or less with what I want, but I do not know much of js and could not filter necessary information in what I read ... THEN!

I wonder how you could make it so that once I selected the <option value="Jurídica"> of my <select>, the <input id="name"> automatically change your placeholder="* Nome Completo" for placeholder="*Nome de sua Empresa" and that in the <input name="cpf"> were added to the property disabled or readonly.

<form>
    <select name="pessoa" id="pessoa">
        <option value="Não especificado">*Tipo de Pessoa</option>
        <option value="Física">Física</option>
        <option value="Jurídica">Jurídica</option>
    </select>

    <input name="nome" id="nome" placeholder="Nome Completo">
    <input name="icpf" id="icpf" placeholder="CPF">
    <input name="icnpj" id="icnpj" placeholder="CNPJ">

Follows form in Jsfiddle http://jsfiddle.net/LhanL/

1 answer

2


You can do it like this: http://jsfiddle.net/z2jfx/

// select
var select = document.getElementById("pessoa");
// input
var input = document.getElementById("nome");

// quando o select muda
select.onchange = function () {
    var valor = select.options[select.selectedIndex].value;
    input.value = valor == 'Jurídica' ? '* Nome de sua Empresa' : '* Nome Completo';
}

This causes when the select changes, it will check whether the chosen value is 'Jurídica'. If the input changes to '* Nome de sua Empresa', otherwise '* Nome Completo'.

Note that these features can/should be done by a library like Mootools or jQuery that takes into account all the different ways browsers (especially old ones) react.

The code above is pure javascript and this is always faster (without loading library), I have now tested on IE7 and IE8.

To disable the input can use so in fa function onchange:

var habilitar = valor == 'Jurídica' ? true : false;
document.getElementById("icpf").disabled = habilitar;
document.getElementById("icnpj").disabled = !habilitar;

But I’m starting to find the solution too personalized.

  • Js is too expensive, I’m studying but it’s so much that I can focus on 1 ... @Sergio but there’s one thing, without wanting to abuse, assuming the fields CPFand CNPJ are Disabledhow do I make it when the person chooses the type of person Física enable the CPF field and when to choose Jurídica enable the field CNPJ? And without wanting to abuse already, can you leave the message "Select the type of person" while the fields are disabled? Thank you in advance!

  • @Viniciuscolares, I forgot the part about disabled. Here it goes. However I begin to think you could do otherwise. You could hide the field instead of being disabled. Then the user doesn’t even notice that the input changes. Or rather, just use an input and check with Regex which number you entered... However jsFiddle: http://jsfiddle.net/z2jfx/1/

  • I liked your idea, but the way it looks is great, I only have a doubt, in the case of this js that you did, when I choose the value Jurídica he adds the property value with the respective value ... it is possible to add valueadd property Placeholder?

  • @Viniciuscolares, yes of course, you can use input.placeholder = valor == 'Jurídica' ? '* Nome de sua Empresa' : '* Nome Completo';.

  • I don’t understand ... I typed this and it didn’t work, then copy the q vc suggested and it worked! UHSAUHSAUHSAHUSAHUASHUAS

  • @Viniciuscolares, boa! If you think the answer solves your problem you can mark as accepted.

  • was bad, had already marked as "accepted" but had given a bug srsrsrsrsrs ?

  • @Viniciuscolares, http://jsfiddle.net/z2jfx/2/

  • Thanks @Sergio you know paranauê ... agr enough abuse, just wanted to know if you have any book or online course that can indicate, so I can learn in a practical and fast way these simple and pure techniques in Js!

  • @Viniciuscolares, this site is the best school. You want to ask good questions, then study the problem better => When the answer comes you are receptive and understand the solution much better.

  • I agree but I feel abusive, because type ... dps who sent me this js I’m more than half an hour trying to change it to leave the form as I said and I can’t, but I’m bothered to keep asking! XD

  • @Viniciuscolares, if it is a new problem or next step of the previous question, and you can’t solve it yourself, so ask another question. There will be people helping.

  • look at this, I was able to do it here ... http://jsfiddle.net/z2jfx/4/ Selecione o tipo de pessoa do select when Physical or Legal is active, you can do this for me?

  • 1

    @Viniciuscolares :) http://jsfiddle.net/z2jfx/5/

  • 1

    =D this is very angry became perfect ... Valew!

Show 10 more comments

Browser other questions tagged

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