0

How to redirect URL when selectbox selected and click Go button? This is what I just tried.

 <script>
        function goCenter(){
            var addrCenter = $("#selCenter").val();
            location.href = addrCenter;

        }
        </script>
        <select id="selCenter"> 
                    <option value='http://ebay.com' selected="selected">ebay</option>
                    <option value='http://google.com'>google</option> 
                </select>

        <button onchange="goCenter();">go</button>

This is not working to me. Please help

1
  • 1
    why not onclick event instead of onchange? Commented Sep 14, 2018 at 2:37

2 Answers 2

2

You can try with onclick instead of onchange. Let's try

        function goCenter(){
            var addrCenter = $("#selCenter").val();
            console.log(addrCenter);
            location.href = addrCenter;
        }
        
        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selCenter"> 
                    <option value='http://ebay.com' selected="selected">ebay</option>
                    <option value='http://google.com'>google</option> 
                </select>

        <button onclick="goCenter();">go</button>

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

Comments

0

You could do this.

$('button').on('click', goCenter);

function goCenter(){
        var addrCenter = $("#selCenter option:selected").val();
        location.href = addrCenter;
    }

You would not need the “onchange="goCenter();" in the button element.

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.