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

April 11, 2013

How to get a radio button’s label text with jQuery

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

From stackoverflow

<input type="radio" name="value" id="product1">
<label for="product1">Product One</label>

You can obtain the label’s text with this jQuery code:

var text = $("input:checked + label").text();

The + signifies the adjacent selector. It will only select a label which has a checked input next to it.

If your label element has to be placed before the input, a single CSS selector can’t get a reference to it. Using jquery, you can however get a reference to the “previous child element” with the prev() function.

var text = $("input:checked").prev().text();

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!


Create a free website or blog at WordPress.com.