2
<script>
function go() 
{
window.location=document.getElementById("menu").value;
}
</script>

<body>
<form>
<select id="menu" onchange="go()">
  <option>--Select a page--</option>
  <option value="http://www.1.com">1</option>
  <option value="http://www.2.com">2</option>
  <option value="http://www.3.com">3</option>
</select>
</form>
</body>

this works perfectly, but I cant call the function from External File

$(document).ready(function() {
    function go(){
window.location=document.getElementById("menu").value;
}});
5
  • what do you mean by external file?? Commented Feb 24, 2014 at 14:18
  • jQuery or JavaScript file Commented Feb 24, 2014 at 14:18
  • You didnt close the document ready function properly...$(document).ready(function() { function go(){ window.location=document.getElementById("menu").value; }}); Commented Feb 24, 2014 at 14:19
  • 1
    Simply call it like you are, just make sure you include the script AFTER your HTML Commented Feb 24, 2014 at 14:19
  • What does the console say? Commented Feb 24, 2014 at 14:22

4 Answers 4

6

By putting function go(){}" inside of "$(document).ready(function() { you enclosing the the go function within the scope of $(document).ready(function() {}

Use document.ready() to call a function, not to declare it.

Sign up to request clarification or add additional context in comments.

Comments

3

Add an Event-Hanlder to your Script like

$('#menu').on('onchange', function()
{
 // do something
}

Example:

$(document).ready(function() {
    $('#menu').on('onchange', function()
    {
       window.location=document.getElementById("menu").value;
    }
});

And your HTML looks like:

<select id="menu">
      <option>--Select a page--</option>
      <option value="http://www.google.com">1</option>
      <option value="http://www.2.com">2</option>
      <option value="http://www.3.com">3</option>
    </select>

Comments

1

No it's jquery but it's very simple!

<select id="menu" onchange="document.location.href = this.value">
      <option>--Select a page--</option>
      <option value="http://www.google.com">1</option>
      <option value="http://www.2.com">2</option>
      <option value="http://www.3.com">3</option>
    </select>

Comments

0

Use $('#menu').find(":selected").text();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.