Stickers
Stickers

Reputation: 78706

jQuery prefill form field on a popup page

My explanation below, hope it's clear.

On the base page, there is a link with ID 123, and click to open enquiry page:

<a href="/enquiry/?id=123"></a>

On the enquiry page, there is a form:

<form>
<input type="text" name="id" value="">
</form> 

Question - Is it possible to pass the ID value "123" to the enquiry page, and prefill the field? make it like below:

<input type="text" name="id" value="123">

Anyone help please, Thanks a lot.

Upvotes: 1

Views: 952

Answers (1)

Harshit Tailor
Harshit Tailor

Reputation: 3281

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);
}

calling getQueryVariable("id") - would return "123".

$('.txt1').val(getQueryVariable("id"));

html :-

<input type="text" name="id" value="" class="txt1">

Upvotes: 3

Related Questions