Dom
Dom

Reputation: 3126

How to grab label value from checked option with jQuery

Im trying to grab value from label checkbox:checked.

I have created this function

 var levels = $('input[name=form[radio1]]:checked + label').map(function() {
  return $(this).text();
}).get();
$('input#radio1_val').val(levels);

and simple html

<input type="radio" id="radio14" value="150" name="form[radio1]">
<label for="radio14">Create simple static website</label>

But is does not seem to work.

Any ideas please?

Many thanks in advance

Dom

Upvotes: 0

Views: 201

Answers (1)

Nick Craver
Nick Craver

Reputation: 630379

What you have works, it just needs a tweak like this:

var levels = $('input:checked + label').map(function() {
  return $(this).text();
}).get();

You can test it here. .get() afterwards just returns the base array afterwards...I think your root issue is you're declaring radio1_val but setting levels.

Upvotes: 2

Related Questions