I have the following code where the alert is working but when I select the option with the given id, nothing happens.
JS:
var connType = (function() {
alert("test");
$("#connType").change(function() {
if ($(this).css("value") === 2 {
$(".mgconn").css("display", "block");
}
});
})();
HTML
<div class="form-group row">
<div class="col-md-3">
<select class="form-control form-control-sm" id="connType" name="connection_type">
<option selected value="1">first op</option>
<option value="2">something</option>
</select>
</div>
</div>
<div class="form-group row mgconn" style="display: none">
<div class="col-md-3">
<input class="form-control input-sm" placeholder="" type="text"/>
</div>
In ide I have the void function highlight, and I don’t understand what it means. I don’t see what return I should have in here. I’m sorry for the simplicity of my question, I realize it must be something basic and I can’t figure really what to look after in the console, to test it.
3
Answers
I have found out that the function was loading before the html because I have another file where is a load() event for the js and I was supposed to call the function there. If the IIFE is removed. the ide warning disappears.
Your IDE warning is because you are assigning the result of a function that returns undefined to a variable.
It’s not a javascript error, it’s just an IDE warning. Depending on your IDE, you’ll probably get the same warning with this code:
You can confirm this with
console.log(connType)
(===undefined) in your code.You can safely remove the
var connType =
part of your code and have the IIFE run without needing to assign to a variable.Here is the working code