0

I'm beginning to use Jquery and I have the following JS function I would like to make with jquery Anyone to help?

<script type="text/javascript" language="javascript">
function redirect() {

  //var dest = "?phone=" + phone;
  var prefill = "&prefill=3";
  var info=document.choice.infos.value;
  var dest = "&info="+info;
  var url=document.choice.centres.value;
  var urlfinal=url+dest+prefill;
  document.location.href=urlfinal
}
</script>


<form name="choice">    
<select name="centres">
  <option value="http://toto.com/?var=1">var1</option>
  <option value="http://toto.com/?var=2">var2</option>
</select>
<br />
<INPUT TYPE="text" NAME="infos">
<input type="button" value="Go!" onClick="redirect();">

</form>
7
  • This sample code is simple enough that it doen't even require you to use jQuery. Commented May 25, 2012 at 19:46
  • 1
    The question you asked is "Would anyone like to help me convert this JS function into a jQuery function." This implies that you have no inherent problem you just want a direct solution to what you are doing. Commented May 25, 2012 at 19:47
  • If you're not sure how to convert it to jQuery, shouldn't you just start learning how to use it? Commented May 25, 2012 at 19:47
  • You should have a problem, something you don't understand, a specific question. What you are basically asking is "can you do my work for me so that I may learn how to use jQuery from your responses." Commented May 25, 2012 at 19:47
  • whats wrong with it in it's current state? you will not get better results with converting it to jQuery, also your function does not have anything that will benefit from the conversion Commented May 25, 2012 at 19:48

2 Answers 2

2

Your button markup

<input type="button" value="Go!" id="submit">

and jquery code:

$(document).ready(function() {
    $('#submit').click(function() {
        //var dest = "?phone=" + phone;

        var finalUrl = $("select#centers").val();
        var querystring = '&prefill=3' + '&info=' 
                               + $("select#info").val() + dest;
        finalUrl = finalUrl + querystring ;
        window.location.replace(finalUrl);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Use #id instead of tag#id for performance reasons. IDs are unique anyway so making it even more specific is not necessary.
I'd use $.param({prefill: 3, info: ...}) instead of building the query string manually.
0

Use Google Chrome or Firefox with the Javascript console open and get in the habit of using console.log to check your code at critical points. By the way, you don't need a form element:

...
<script type="text/javascript">
$(function () {

    $('#btnRedirect').click(function () {
        var centres = $('#centres');
        var centre = $('option:selected', centres);
        //console.log(centre.val());

        var infos = $('#infos');
        //console.log(infos.val());

        var dest = "&info=" + infos.val();
        var prefill = "&prefill=3";
        var urlfinal = centre.val() + dest + prefill;
        //console.log(urlfinal);

        document.location = urlfinal;
    });
});
</script>

...

<body>
<select id="centres">
  <option value="http://toto.com/?var=1">var1</option>
  <option value="http://toto.com/?var=2">var2</option>
</select>
<br />
<input type="text" id="infos"/>
<input id="btnRedirect" type="button" value="Go!"/>
</body>

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.