billah77
billah77

Reputation: 194

how to get value query string in javascript and put it into a textbox

I have a query string in javascript which looks like this : Default.aspx?7KCN0008-001 and when i try to insert 7KCN0008-001 into the textbox i get [object Object] how can i put the value inside the textbox here is what i have:

function test() {
    var urlParams;
    (window.onpopstate = function () {
        var match,
            pl = /\+/g,  // Regex for replacing addition symbol with a space
            search = /([^&=]+)=?([^&]*)/g,
            decode = function (s) { return decodeURIComponent(s.replace(pl,"")); },
            query = window.location.search.substring(1);

        urlParams = {};
        while (match = search.exec(query))
            urlParams[decode(match[1])] = decode(match[2]);
    })();
 $('#txtStockCode').val(urlParams[decode(match[1])]);
}

Upvotes: 0

Views: 491

Answers (2)

naota
naota

Reputation: 4718

I might be barking up the wrong tree, but as you wrote in your question, if URL is something like Default.aspx?7KCN0008-001, you could get the string 7KCN0008-001 by this code.

 query = window.location.search.substring(1);

So I guss you could simply put this string into the textbox

$('#txtStockCode').val(query);

The document of location.search is here:
http://www.w3schools.com/jsref/prop_loc_search.asp

Hope this helps.


EDIT:

Okay, if the query is 7KCN0008-001 SAVSS0.85B 2180 916941-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20 917418-060, I guess you could do something like this:

HTML:

<input type="text" id="t1">
<input type="text" id="t2">
<input type="text" id="t3">
<input type="text" id="t4">
<input type="text" id="t5">
<input type="text" id="t6">

Javascript:

var query = window.location.search.substring(1); // "7KCN0008-001 SAVSS0.85B ...."
var decoded = decodeURI(query);
var ary = decoded.replace(/\s{2,}/g, ' ').split(' '); 
for (var i = 0; i < ary.length; i++) { 
  $("#t"+(i+1)).val(ary[i]);
}

DEMO JSfidle is here:http://jsfiddle.net/naokiota/pH4mB/2/

Hope this helps.

Upvotes: 1

Sten Muchow
Sten Muchow

Reputation: 6701

I think you are confuse about the data structure. You first define it to be an Object and then you say its an array.

urlParams = {};

urlParams[decode(match[1])] = decode(match[2]);

You need to find out the value of decode(match[1]) and use this in the .val method as its an array....

$('#txtStockCode').val(urlParams[decode(match[1])]);

Upvotes: 0

Related Questions