4

I have an input as below and I would like to prefill the value with the value of the query string so that when I access example.com/my.html?phone=12345 the value 1234 should be displayed in the input field. Sorry if my question seems stupid but I have no kind of experience with jquery and javascript

<input type="phone" value="" class="form-control" id="phonenumber"
placeholder="Phone number">
2
  • which server side language (php, jsp, asp) using?
    – Girish
    Commented Aug 7, 2014 at 8:57
  • Perhaps you could also do this on the server side?
    – Scimonster
    Commented Aug 7, 2014 at 8:58

4 Answers 4

9

You can use this script:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

And for setting the values:

$("#phonenumber").val(getParameterByName("phone"));

Reference: How can I get query string values in JavaScript?

Fiddle: http://jsbin.com/hikoyaki/1?phone=12345

5
  • You can also loop through all the inputs in the form: $('#someform input.form-control').each(function () { var name = $(this).attr('name'); var val = getParameterByName(name); $('input[name="' + name + '"]').val(val); });
    – Halceyon
    Commented Sep 20, 2018 at 5:32
  • @Halceyon yep, possible. Commented Sep 20, 2018 at 11:36
  • It shows the code, but the query string is not updating any info.
    – kwyntes
    Commented Dec 3, 2018 at 7:16
  • @qjnr You need to know how JSBin works. There's a clear message. Please fork and try it out. :) Commented Dec 3, 2018 at 9:17
  • Oh, my bad! I didn't see it said 'Expired'! :P
    – kwyntes
    Commented Dec 3, 2018 at 15:47
1

Use javascript split()

var test = 'example.com/my.html?phone=12345';
var res = test.split("=");
$("#phonenumber").val(res[1]);

Note: Use this if you have only one query string in url.

4
  • in case ?name=test&phone=12345 ??
    – Girish
    Commented Aug 7, 2014 at 9:01
  • Yes @Girish this is for OP if he have only one query parameter phone
    – Manwal
    Commented Aug 7, 2014 at 9:01
  • there can also # with 1 param, you should provide vital solution.
    – Girish
    Commented Aug 7, 2014 at 9:03
  • This solution is for OP only regarding example.com/my.html?phone=12345 situation
    – Manwal
    Commented Aug 7, 2014 at 9:05
1

Here is a Plain javascript way of doing it

CSS-TRICKS

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
2
  • The question is clearly tagged with jQuery and NOT JavaScript. Commented Aug 7, 2014 at 11:03
  • 8
    if Javascript can do it then why bother with jquery ? Commented Aug 7, 2014 at 11:20
-2

Below example may help you

var location = 'example.com/my.html?phone=12345';
var phoneNo = location.split("?phone=")[1];
2
  • I guess the whole thing is about how to get it from the query string not from a string
    – hey
    Commented Aug 7, 2014 at 8:59
  • there is no test variable in the solution given
    – saikiran
    Commented Aug 7, 2014 at 10:54

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.