a developer's notes – a semi-technical web development BLOG

April 11, 2013

Parsing a Query String into an array with JavaScript

Filed under: Javascript / JQuery — Duy Nguyen @ 9:50 pm
Tags: , , , , , , , , ,

This brilliant method belongs to Joe Zim’s JavaScript Blog.

Here is the function.

    var parseQueryString = function( queryString ) {
        var params = {}, queries, temp, i, l;
     
        // Split into key/value pairs
        queries = queryString.split("&");
     
        // Convert the array of strings into an object
        for ( i = 0, l = queries.length; i < l; i++ ) {
            temp = queries[i].split('=');
            params[temp[0]] = temp[1];
        }
     
        return params;
    };

Using the function.

   function Test() {
        var qstring = "apple=7sf&orange=242&bananna=47614&cherry=8139";
        var myNewArray = parseQueryString(qstring);
    }

As you can see, you now have an array of all of your query strings!

parseQstring

Advertisements

September 5, 2011

Get a Javascript DOM object from a jQuery object

Filed under: Javascript / JQuery — Duy Nguyen @ 1:49 pm
Tags: , , , , ,

jQuery object is a wrapper around the js DOM object. If you want to get the DOM object from the jQuery object, you should use get() method.

For example, if the jQuery object includes only one DOM object, you can use

$("div").get(0)

or

$("div")[0]

However, if you have multiple DOM objects, you can use

$("div").get()

var myvar;
myvar= $(‘#<%=selectedProductionIdsHdnVl.ClientID%>‘).get(0);
myvar.value = “b”;

same as

document.getElementById(“<%=selectedProductionIdsHdnVl.ClientID%>“).value = “B”;

This allowed me to use the ASP Hidden field and have the OnValueChanged server side event get fired.

Blog at WordPress.com.