0

I think I am trying something very easy but it does not work on Chrome but on Firefox.

Depending on the value of my combo box order-type I want to show the field nameField. Therefore, I have a JavaScript file, which has a Listener on changes. When I change now a value in the combo box in Firefox the nameField appears and I get a console message. In Chrome I don’t get anything at all.

My PHP file looks like this:

                            <select name="order-type" id="order-type">
                                    <option selected="selected">Choose one</option>
                                    <?php
                                    $products = array("Value1", "Value2", "Value");

                                    foreach($products as $item){
                                    ?>
                                    <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>
                                    <?php
                                    }
                                    ?>
                            </select>
                        </p>
                        <div id="nameField" style="display: none;">
                            <label for="userName">User name *</label>
                            <input id="userName" name="userName" type="text" class="required" autocomplete="username">
                        </div>
…
<script src="js/script.js" defer></script>

Script.js:

    var form = $("#my-form");

form.validate({
    errorPlacement: function errorPlacement(error, element) { element.before(error); },
    rules: {
        confirm: {
            equalTo: "#password"
        }
    }
});
form.children("div").steps({
    headerTag: "h3",
    bodyTag: "section",
    transitionEffect: "slideLeft",
    onStepChanging: function (event, currentIndex, newIndex)
    {
        form.validate().settings.ignore = ":disabled,:hidden";
        return form.valid();
    },
    onFinishing: function (event, currentIndex)
    {
        form.validate().settings.ignore = ":disabled";
        return form.valid();
    },
    onFinished: function (event, currentIndex)
    {
        form.submit();
    }
});

var comField = document.getElementById("order-type");

comField.addEventListener("change", function(){

    console.log("Combox Box");
    console.log(comField.value);
  if(comField.value.toLowerCase() == ("Value1").toLowerCase()){
    console.log("Value1");
    document.getElementById("nameField").style.display = "block";
  } else {
    console.log("other");
    document.getElementById("nameField").style.display = "none";

  }
});

Any ideas?

Thanks

Stephan

Stephan
  • 315
  • 2
  • 12
  • Works fine for me: https://jsfiddle.net/barmar/dgvn0wxs/1/ – Barmar Aug 02 '19 at 18:59
  • You don't even get the `Combo Box` log message? – Barmar Aug 02 '19 at 19:00
  • Hi Barry, No, I don’t get anything, not even the Combo Box message. Is it possible that it has something to do with my environment? I am using docker container and laravel. Other functions in the javascript file are working but never does the console log. I updated my complete javascript file, even when I think the form parts shouldn’t disturb. – Stephan Aug 02 '19 at 19:39
  • If none of your `console.log()` calls are working, there's probably something in the environment intercepting it, not sure what that would be. But that shouldn't affect `addEventListener`. What happens if you change `console.log` to `alert`? – Barmar Aug 02 '19 at 19:45
  • The code worked well, I think you should try to check out your server-side code – Afshin Izadi Aug 02 '19 at 19:50
  • So, I didn't change anything, only I turned my docker container off and on again and now it is working... any explanations? It seems that Chrome didn't reload the Javascript file probably: https://stackoverflow.com/questions/37873112/how-to-update-laravel-project-after-changing-javascript-file-and-controller Thanks for your help – Stephan Aug 03 '19 at 16:11

0 Answers0