0

I am looking for a safe way to get value of a selected radio button :

  • I do not know the form id
  • I do not know how much similar (ids excluded) form I have in my page

I am currently using this :

if (($(elem).is('input[type="radio"]')) && ($(elem).attr('name') !== undefined)) {
    return $('input[name="' + $(elem).attr('name') + '"]:checked').val();
}

But this is not working if I have similar names in distinct forms, and I don't even know if this is safe to put such a thing on a selector.

My goal is to create a getValue() function that returns value of one element ($(elem).length == 1) without a matter of nature :

var getValue = function(elem) {
    if ($(elem).is('input[type="checkbox"]')) {
        return ($(elem).prop('checked') == true);
    }
    else if (($(elem).is('input[type="radio"]')) && ($(elem).attr('name') !== undefined)) {
        return $('input[name="' + $(elem).attr('name') + '"]:checked').val();
    }
    else if ($(elem).prop('value') !== undefined) {
        return $(elem).val();
    } else {
        return $(elem).html();
    }
}

Any suggestion ?

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153

2 Answers2

0

You can use jquery to get the value with less effort. There are good range selectors in jquery which you can use to select button. For eg

$("#some_div #inside_div formelement").val();

All you have to know is a parent container Id atleast.

KA.
  • 4,220
  • 5
  • 26
  • 37
  • sorry my bad, you are using jquery ofcourse.. if you know any parent id you can get value. – KA. Oct 30 '12 at 06:59
0

I found a solution, using this answer :

// Get selector from any dom element
// @see https://stackoverflow.com/a/2068381/731138
var getPath = function(elem) {
    var path;
    var node = $(elem);
    while (node.length) {
        var realNode = node[0];
        var name = realNode.localName;
        if (!name) {
            break;
        }
        name = name.toLowerCase();
        if (realNode.id) {
            return name + '#' + realNode.id + (path ? '>' + path : '');
        } else if (realNode.className) {
            name += '.' + realNode.className.split(/\s+/).join('.');
        }
        var parent = node.parent();
        var siblings = parent.children(name);
        if (siblings.length > 1) {
            name += ':eq(' + siblings.index(node) + ')';
        }
        path = name + (path ? '>' + path : '');
        node = parent;
    }
    return path;
}

// Return value of any element without knowledge of its type
var getValue = function(elem, contextSelector) {
    if ($(elem).is('input[type="checkbox"]')) {
        return ($(elem).prop('checked') == true);
    }
    if (($(elem).is('input[type="radio"]')) && ($(elem).attr('name') !== undefined)) {
        return $(contextSelector + '>input[name="' + $(elem).attr('name') + '"]:checked').val();
    }
    if ($(elem).prop('value') !== undefined) {
        return $(elem).val();
    } else {
        return $(elem).html();
    }
}

Then, when I need to recover fields to post from any selector, this looks like :

// Get all data to post
var elemSelector = getPath(elem);
var inputSelector = $(elem).data('input');
var inputData = {};
if (inputSelector !== undefined) {
    if (inputSelector !== '') {
        inputSelector = inputSelector + ' :input';
        $.each($(inputSelector), function (index, node) {
            if (($(node).attr('name') === undefined) && ($(node).attr('id') === undefined)) {
                return true;
            }
            var key = $(node).attr('name');
            if (key === undefined) {
                key = $(node).attr('id');
            }
            inputData[key] = getValue(node, elemSelector);
        });
    } else {
        inputData['value'] = getValue(elem, elemSelector);
    }
}
Community
  • 1
  • 1
Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153