-2

I have a requirement where i have a form, Input text and a button. On button click i need to call two URL on condition. Is this possible through JavaScript.

For example -

When i click button, if the input text is India the URL should go to http://google.co.in

If the input text is UK the URL should go to http://google.co.uk

What are the ways we can implement this requirement.

<form name="inputform" action="somewhere" method="post">
    <input type="text" value=""  />
    <input type="button"  />
</form>

Thanks in Advance Regards

user2542953
  • 127
  • 1
  • 9

2 Answers2

0

you can try something of this sort :

  <form name="inputform" action="somewhere" method="post">
        <input type="text" value=""  id="t"/>
         <input type="button" onclick="chkform()" />
 </form>

     <script type="text/javascript">
function chkform(){
        var x =  document.getElementById("t").value;    
        if(x==="India"){
                window.location="http://www.google.com";        
        }   
        else if(x==="UK"){
                window.location="http://www.google.co.uk";  
       }    
  }     

     </script>
5eeker
  • 1,016
  • 1
  • 9
  • 30
0
$('form').on('submit', function() {
    var inputVal = $('input[type="text"').val();
    if (inputVal === "India") {
        window.location.replace("http://google.co.in");
    } else if (inputVal === "UK") {
        window.location.replace("http://google.co.uk");
    }
});

It would help a lot if these HTML elements were given class or ID attributes to identify them further but this code is a working mashup of JS and jQuery (as you tagged them both) of what I think you want.

N.B: If you are adding mote conditions, (instead of just India and UK) it may be worth using a switch statement as they look more readable

This code assumes that your button is the submit button for your form

wmash
  • 4,032
  • 3
  • 31
  • 69