Change field from hidden to text with javascript

1

All good personal?

So, I have a field of type hidden, I need to assign a value to it and change it from hidden to text after the calculation

function calc(){
    var myVar = formCalc.myCamp.value;
    document.getElementById("calcFeito").value = myVar*2;
    document.getElementsByTagName("hidden").setAttribute("type", "text");
}   


<form name="formCalc">
    <br>Digite o Valor:<br>
    <input type="text" name="myCalc">
    <input type="button" value="botão" onClick="calc()"></button><br>
</form>
<input type='hidden' id='calcFeito'>
    
asked by anonymous 18.07.2016 / 07:06

2 answers

6

The function document.getElementsByTagName is not used to search by attributes, I recommend that you read the documentation, the use is totally wrong, this search function by tag name, if you want to pick up elements more easily you can use document.querySelector , or even document.getElementById .

Also, as MagicHat mentioned, the tags are incorrect, have a </button> left, always try to make the marking as correct as possible, you can use tools like:

  

Note: you do not need to validate 100%, worry about looking at validations on wrong tags and repeated ids (another thing that can cause problems)

See here I've adapted everything to getElementById :

function calc(){
    var myVar = document.getElementById("myCalc").value;
    document.getElementById("calcFeito").value = myVar * 2;
    document.getElementById("calcFeito").setAttribute("type", "text");
}  
<form name="formCalc">
<br>Digite o Valor:<br>
<input type="text" name="myCalc" id="myCalc">
<input type="button" value="botão" onClick="calc()"><br>
</form>
<input type='hidden' id='calcFeito'>

Read the documentation on DOM and learn how each function works before using it:

18.07.2016 / 08:06
0

You can do it in two ways using jQuery.

First, you change the type=hidden to type=text using the .attr ()

Example:

/*Para mostrar*/
$("#calcFeito").attr('type', 'text');

/*Para ocultar*/
$("#calcFeito").attr('type', 'hidden');

Or use own .show() and .hide() of jQuery.

Having <input type='text' id='calcFeito' style='display:none'> as the base:

/*Para mostrar*/
$("#calcFeito").show();

/*Para ocultar*/
$("#calcFeito").hide();
    
18.07.2016 / 07:17