-2

Hello trying to write jquery that when l select a direction from my html drop down menu it provides an alert saying the name of direction l chose. Tried the below code but seems to not work

$(document).ready(function() {
    
    //selection for combobox...
    $('#mydirection').change(function (e){
        doOnChange(e)
    })

});

function doOnChange(myObj){
    if (typeof console == "E"){
        console.log("Change on selection");
        console.log(myObj);
    }
    alert("You chose East");

    if (typeof console == "W"){
        console.log("Change on selection");
        console.log(myObj);
    }
    alert("You chose west");

    if (typeof console == "N"){
        console.log("Change on selection");
        console.log(myObj);
    }
    alert("You chose north");

    if (typeof console == "S"){
        console.log("Change on selection");
        console.log(myObj);
    }
    alert("You chose south");
}
function updateStatusBox(img, msg) {
    document.getElementById('statuscaption').innerHTML = msg;
    document.getElementById('event_image').src = img;
}
<p>Please select a direction:<br/>
    <select id="mydirection"  name="direction">
      <option value="N">North</option>
      <option value="S">South</option>
      <option value="E">East</option>
      <option value="W">West</option>
    </select>
  </p>
  • 1
    How exactly does it _not work_? What happens vs what do you expect to happen? Are there any errors reported? Your question snippet is missing jQuery, do you have the same issue in your own code? – Phil Apr 07 '22 at 05:53
  • You have a lot of errors. Why are you comparing `typeof console` to E, W, etc (`typeof console` is **always** `"object"`)? You're passing the event `e` as the `myObj` argument but you never use it – Phil Apr 07 '22 at 05:56

1 Answers1

0

This may help you!!

$(document).ready(function() {
    //selection for combobox...
    $('#mydirection').change(function (e){
        doOnChange($('option:selected',this))
    })

});

function doOnChange(myObj){
    console.log(myObj.text())
}
function updateStatusBox(img, msg) {
    document.getElementById('statuscaption').innerHTML = msg;
    document.getElementById('event_image').src = img;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Please select a direction:<br/>
    <select id="mydirection"  name="direction">
      <option value="N">North</option>
      <option value="S">South</option>
      <option value="E">East</option>
      <option value="W">West</option>
    </select>
  </p>
Nirav Joshi
  • 2,924
  • 1
  • 23
  • 45