skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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:

    function example() {
    }
    var x = example()
    

    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.

    Login or Signup to reply.
  3. Here is the working code

    <!DOCTYPE html>
        <html>
           <head>
          <script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
           </head>
           <body>
              <div class="form-group row">
                 <div class="col-md-3">
                    <select class="form-control form-control-sm" id="connType" 
                       name="connection_type">
                       <option selected>first op</option>
                       <option value="something">something</option>
                    </select>
                 </div>
                 <div class="mgconn" style="display:none"> Show on change</div>
              </div>
              <script>
                 $("#connType").change(function(){
                     if($(this).val() === "something"){
                         $(".mgconn").css("display","block");
                      }else{
                         $(".mgconn").css("display","none");
                      }
                 });
              </script>
           </body>
        </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search