Reputation: 342
In an attempt to abbreviate code and perhaps make it run a touch faster, I tried this code for getting the value if an input whose name is key
is a radio button:
var elem = $(':input[name^="'+key+'"]');
currVal = (elem.prop('type') == 'radio' ?
':input[name^="'+key+'"]:checked').val() : elem.val());
I've tried various ways to abbreviate the instruction
':input[name^="'+key+'"]:checked')
but nothing works.
Is there a way of using elem
in this situation?
Upvotes: 0
Views: 37
Reputation: 688
Here's a working fiddle using your code: https://jsfiddle.net/wvbxmgpe/
var elem = $(':input[name^="'+key+'"]'),
currVal = elem.prop('type') == 'radio'
? $(':input[name^="'+key+'"]:checked').val()
: elem.val();
Upvotes: 0
Reputation: 207527
Well you already have the element with
var elem = $(':input[name^="'+key+'"]');
So instead of looking up all the elements again, use filter
elem.filter(":checked").val()
Upvotes: 2
Reputation: 438
My guess is $("input[name='+key+'][type='radio']:checked").val();
should work for you.
Upvotes: 0