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? Commented Aug 7, 2014 at 8:57
  • Perhaps you could also do this on the server side? 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

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

5 Comments

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 yep, possible.
It shows the code, but the query string is not updating any info.
@qjnr You need to know how JSBin works. There's a clear message. Please fork and try it out. :)
Oh, my bad! I didn't see it said 'Expired'! :P
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 Comments

in case ?name=test&phone=12345 ??
Yes @Girish this is for OP if he have only one query parameter phone
there can also # with 1 param, you should provide vital solution.
This solution is for OP only regarding example.com/my.html?phone=12345 situation
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 Comments

The question is clearly tagged with jQuery and NOT JavaScript.
if Javascript can do it then why bother with jquery ?
-2

Below example may help you

var location = 'example.com/my.html?phone=12345';
var phoneNo = location.split("?phone=")[1];

2 Comments

I guess the whole thing is about how to get it from the query string not from a string
there is no test variable in the solution given

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.