fiterpilot
fiterpilot

Reputation: 27

How would I find the label's child? (HTML + JS)

How would I make JS find the child with the class of "money"? I have it coded so that it will find the radio button when clicked. Once it's clicked it will return that radio button and id. But I also need to find the "money" class.

HTML:

<div class="col-sm-4 col-md-2">
  <input name="STORAGE" type="radio" id="STORAGE2">
  <label class="radio" for="STORAGE2">
    <center>
      <img src="../images/Parts/Western_Digital_Caviar_Blue_320GB.png" class="comp-img"><br><br>
      <p>500GB Western Digital WD5000AAKX 3.5" 7200RPM HDD</p>
      <p class="money">+$55</p> <!--THIS IS THE CLASS I AM TRYING TO GET-->
    </center>
  </label>
</div>

JS:

$(function(){
  var cpu = "";

  //Controling radio buttons
  $('radio').on('click'), function(){
    var clickedID = $(this).attr("id");
    if (clickedID.substring(1,3) == "CPU") {
      if (cpu == "") {
        cpu = clickedID;

      } else {

      }
    }
  }
});

Upvotes: 0

Views: 108

Answers (3)

Drey
Drey

Reputation: 130

Your syntax is off a little bit, but this should work:

EDIT:

As @empiric has noted, .money is not a child of $('radio'), it is a child $('.radio'), which is not the intended click target.

$('.radio').on('click', function(){
    var $this = $(this);
    var clickedID = $this.attr("for");
    // Child .money element (put this wherever you need it)
    var childMoneyEl = $this.find('.money')


    if (clickedID.substring(1,3) == "CPU") {
        if (cpu == "") {
            cpu = clickedID;

        }else{

        }
    }
});

Upvotes: 0

M B
M B

Reputation: 2326

if your label's for attribute is the value of the radio ID then you can do like this

var el = $('label[for='+clickedID +']').find('.money');

Upvotes: 3

aleksa95
aleksa95

Reputation: 69

use the jQuery find function $(this).find(".money");

Upvotes: 0

Related Questions