4

Hello Everyone I want to capture label name(72*30) using the following code:

$("input[type = 'radio']:checked:last").each(function() {
  var idVal = $(this).attr("id");
  var a = $("label[for='"+idVal+"']").text();
  alert(a);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="radio">
  <input class="tmcp-field nav nav-pills dimension-layer-dimension  tm-epo-field tmcp-radio" type="radio">
  <label id="tmcp_radio_9" >72*30</label>	
  <span class="price tc-price  hidden">
    <span class="amount">20 <i class="fa fa-inr"></i></span>
  </span>		
</li>

But the code doesn't alert anything. What is wrong?

3
  • you can directly get the value using id..? then why Commented Sep 30, 2016 at 8:45
  • 1
    You have given an id to label and can access directly using $("#tmcp_radio_9").text() Commented Sep 30, 2016 at 8:46
  • @KeerthiVasan i have three radio button id is generating dynamically same Commented Sep 30, 2016 at 8:48

3 Answers 3

6

You can use next() because your label is after (next) your input

$("input[type='radio']:checked:last").next().text()

Hope this will help you.

You can see demo here

Sign up to request clarification or add additional context in comments.

Comments

0

Use this keyword

$("input[type='radio']:checked").each(function() {

   var a= $(this).next().text();
   alert(a);

});

Comments

0

Try this also

$("input[type='radio']:checked").each(function() {

   var a= $(this).closest("label").text();
   alert(a);

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.