3
Making a Javascript code recently, I found that when using a Function, when I put THIS as a parameter it was not executed, but when putting OBJ, it worked normally. Can someone tell me the difference between the two?
function mostraCampo(obj) {
var select = document.getElementById('instituição')
if (select.value == 'OUTRA') {
document.getElementById("outrainst").style.visibility = "visible";
} else{
document.getElementById("outrainst").style.visibility = "hidden";
}
}
<div class="form-group">
<label> Instituição de ensino <br />
<select class="form-group" name="instituição" id="instituição" onchange="mostraCampo(this.value);">
<option value="UFTM">UFTM</option>
<option value="UNIUBE">UNIUBE</option>
<option value="FACTHUS">FACTHUS</option>
<option value="SENAI">FAZU</option>
<option value="IMEPAC">IMEPAC</option>
<option value="NENHUMA">NENHUMA</option>
<option value="OUTRA">OUTRA</option>
</select>
<input type="text" class="form-control" name="outrainst" id="outrainst" style="visibility: hidden;">
</label>
</div>
PHP code or Javascript? By the way, which code? What is
this
and what isOBJ
in this example? Please edit your question and be clearer on the problem, especially by adding the code that generated the doubt.– Woss
It’s edited, I forgot to mention that are the parameters of Function! And I didn’t know how to put the code.
– Arthur Oliveira
In advance:
this
is an object that represents the current element, alreadyobj
, in this case, it is the argument of the function. They are very different things. See if this helps you: https://answall.com/questions/233628/como-utili-o-this-maneira-correct– Woss
I think I understood, for example, to use this as I used to call the function within the right select because it will refer to select, but to use this as an argument of the function it will understand this as a variable would be this?
– Arthur Oliveira
Basically this, so much so that in this case
obj
will be thethis.value
ofselect
, then, in the job, all you had to do wasif (obj == "OUTRA") ...
, without needing in thevar select
.– Woss
Got it, thank you very much!!!
– Arthur Oliveira