-3

There is a three level dependent drop-down(country-state-city) using jquery, AJAX and JSON. To redirect the user to a page after he selected city (www.example.com/city, the city is numeric value here) I used the following code, HTML

<select name="city" id="city" class="form-control input-lg" onchange="onfuct()">
 <option value="">Select City</option>
</select>

The javascript is here

var value_1 = document.getElementByID('city').value;

function onfuct () {
   window.location.replace("www.example.com"/+value_1);
}

Can somebody help me out it is redirecting to "www.example.com/undefined"

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
madhurkant
  • 96
  • 9

1 Answers1

1

value does not exist on <select>, you need to do a bit more work to obtain the selected value. Also, JavaScript is case-sensitive; getElementByID is NOT getElementById

// Get the element from DOM
const city = document.getElementById('city');

/*
  Move event listeners to JavaScript like

  city.addEventListener('onchange', onfuct)

*/

function onfuct() {
  // city.options returns the option elements
  let selectedCity = city.options[city.selectedIndex].value;

  console.log("www.example.com/" + selectedCity);
}
<select name="city" id="city" class="form-control input-lg" onchange="onfuct()">
  <option value="stockholm">Stockholm</option>
  <option value="new-york">New York</option>
  <option value="oslo">Oslo</option>
</select>
Adam Azad
  • 11,171
  • 5
  • 29
  • 70